动态选择的元素未被选中

时间:2016-02-03 08:24:27

标签: c# dynamic drop-down-menu selenium-webdriver element

有关此代码段未选择单击元素的任何想法。我正在使用代码动态选择元素,它做得很好但是在选择点击的元素时它并没有。可能有什么不对?请帮忙。

IList<IWebElement> DynamicDatasource = driver.FindElements(By.XPath("//div[@id='s2id_Parameters_0_Validation_TheFormat']//span[starts-with(@id, 'select2-chosen-')]"));
if (DynamicDatasource.Count > 0)
{
    File.WriteAllText(@"C:\Users\ken4ward\Desktop\Tidy\WriteLines.txt", DynamicDatasource.Count.ToString());
    Actions actions = new Actions(driver);
    actions.MoveToElement(DynamicDatasource[0]).Click().SendKeys("Real Integer").Build().Perform();
    IList<IWebElement> standard = driver.FindElements(By.XPath("//div[@class='select2-drop select2-display-none select2-with-searchbox select2-drop-active']//ul[starts-with(@id, 'select2-results-')]"));
    if (standard.Count > 0)
    {
        File.WriteAllText(@"C:\Users\ken4ward\Desktop\Tidy\Writing.txt", DynamicDatasource.Count.ToString());
        actions.MoveToElement(standard[0]).Click().Build().Perform();
    }
}

1 个答案:

答案 0 :(得分:0)

据我所知,你是一个复合班。找到复合类可能无法正常工作。而是尝试Cssselector。

IList<IWebElement> standard = driver.FindElements(By.XPath("//div[@class='select2-drop select2-display-none select2-with-searchbox select2-drop-active']//ul[starts-with(@id, 'select2-results-')]"));

所以请尝试以下希望它应该工作

IList<IWebElement> standard = driver.FindElements(By.CssSelector(".select2-drop.select2-display-none.select2-with-searchbox.select2-drop-active"));