如果我想选择下拉框的选项,有几种方法可以做到这一点。我一直用:
driver.findElement(By.id("selection")).sendKeys("Germany");
但这并不是每次都奏效。有时会选择另一个选项。所以我google了一下,发现这段代码每次都有效:
WebElement select = driver.findElement(By.id("selection"));
List<WebElement> options = select.findElements(By.tagName("option"));
for (WebElement option : options) {
if("Germany".equals(option.getText()))
option.click();
}
但这真的很慢。如果我的 list 很长,里面有很多项目,那真的需要太多时间。所以我的问题是,是否有一种解决方案每次都有效且速度快?
最佳答案
你可以试试这个:
IWebElement dropDownListBox = driver.findElement(By.Id("selection"));
SelectElement clickThis = new SelectElement(dropDownListBox);
clickThis.SelectByText("Germany");
关于java - Selenium WebDriver 和下拉框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7232544/