如果我想选择一个下拉框选项,有几种方法可以做到这一点。我总是习惯:Selenium WebDriver和DropDown Boxes
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();
}
但是这样做的确很慢。如果我有一个很长的名单,其中有很多项目,它真的需要太多时间。所以我的问题是,是否有解决方案,每次都有效,速度很快?
我认为这是一些C#代码的东西?但它帮助我找出以下代码: WebElement dropDownListBox = driver.findElement(By.id(“selection”)); \t \t选择clickThis = new Select(dropDownListBox); \t \t clickThis.selectByValue(“Germany”); 快得多!谢谢! – tester
应该为IWebElement和SelectElement导入哪个包? –
谢谢测试人员,该代码适用于硒2。 –