1

我正在使用 Selenium 3.0 和 firefox 48 来自动化应用程序。但是在 Firefox48 中,自动选择下拉菜单不起作用。

相同的代码适用于 IE 和 chrome。

这是浏览器问题还是我的代码问题?

在此处输入图像描述

Select sel = new Select(driver.findElement(By.xpath("//select[contains(@id,'BusinessUnit')]")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ctl00_vmsContent_rdwBusinessUnit_C_selBusinessUnit")));
List<WebElement> list = sel.getOptions();
for (WebElement el : list)
{
    System.out.println(el.getText());
    sel.selectByIndex(2);
}
4

1 回答 1

0

我会稍微简化一下代码。我添加了一些用于调试目的的代码。

// wait until returns a WebElement, store it for later use
WebElement e = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[contains(@id,'BusinessUnit')]")));
// dump the HTML of the select element and make sure you have the element you are expecting
System.out.println(e.getAttribute("outerHTML"));
Select sel = new Select(e);
for (WebElement el : sel.getOptions())
{
    System.out.println(el.getText());
}
sel.selectByIndex(2); // pull this out of the loop or it will get selected mutliple times
// other options for selecting the desired OPTION
sel.selectByValue("12");
sel.selectByVisibleText("Engineering");
于 2016-09-16T18:03:01.247 回答