我正在将我的 selenium 1 代码转换为 selenium 2,但找不到任何简单的方法来在下拉菜单中选择标签或获取下拉菜单的选定值。你知道如何在 Selenium 2 中做到这一点吗?
以下是在 Selenium 1 中有效但在 2 中无效的两个语句:
browser.select("//path_to_drop_down", "Value1");
browser.getSelectedValue("//path_to_drop_down");
最佳答案
查看关于 filling in forms 的部分使用 selenium 文档中的 webdriver 和 Select 的 javadoc类。
根据标签选择选项:
Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));
select.deselectAll();
select.selectByVisibleText("Value1");
获取第一个选择的值:
WebElement option = select.getFirstSelectedOption()
关于java - 如何在 Selenium 2 中选择/获取下拉选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6430462/