Selenium Webdriver无法选择DropDown值

时间:2017-11-22 10:11:02

标签: java selenium dropdown

我正在尝试选择显示的值在展开DOM为

的下拉列表时
<a class="mcacAnchor">
<span style="padding:0 4px;float:left;width:8em;">L</span>
<div style="clear: both;"/>
</a>

现在,下拉列表会点击扩展点击&#34; L&#34;但是这不会(值L)进入包含下拉列表的字段。

令人惊讶的是执行进一步发展。

我尝试过Xpath,包含文本,但似乎没有任何效果。

处理它的代码:

WebElement Icon=driver.findElement(By.xpath(".//input[@id='FlatID']/..//span"));
        executor.executeScript("arguments[0].click();", Icon);
        driver.findElement(By.xpath(".//span[contains(text(),'L')]")).click();

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试对同一个对象click()执行L操作。

  

我正在尝试选择展开A下拉列表时显示的值

您可能应首先点击下拉列表

WebElement dropdown = driver.findElement("//a[@class='mcacAnchor']")
  

现在,下拉列表获得Expanded,点击“L”,但不会(包含L)进入包含下拉列表的字段。

现在,下拉列表已展开,您可以对click()元素执行L操作。

WebElement lElement = dropdown.findElement(By.xpath(".//span[contains(text(),'L')]"));
lElement.click();

在上面一行中,我们没有使用driver来查找元素,而是使用dropdown,因为它会将findElement方法的范围缩小到此特定dropdown。 xpath中的The .//表示perform search reduced to provided scope

但是,根据您的应用程序,Selenium可能会对不可见的click元素执行L,因为它可能比浏览器更快。它将打开下拉列表,但不会等到显示下拉列表。

可以使用WebDriverWait实现此类等待。

您的代码示例(以及最终解决方案):

WebElement dropdown = driver.findElement("//a[@class='mcacAnchor']")
WebElement lElement = dropdown.findElement(By.xpath(".//span[contains(text(),'L')]"));
WebDriverWait wait = new WebDriverWait(driver, 10); //it will wait 10 seconds for desired condition
wait.until(ExpectedConditions.visiblityOf(lElement));
lElement.click();