我正在使用 Selenium 进行自动化。我正在使用 DefaultSelenium
类,在我们的应用程序中我有一个下拉菜单。我想从这个下拉菜单中获取一个值。
最初,我使用 selenium IDE 编写了脚本,它给我的代码如下:
selenium.select("id=skuOptionSIZE1c4b403", "label=8");
但是当我开始编写代码 (Java) 时,Eclipse 会抛出错误,而我仍然能够看到页面上存在的下拉 id
:
Exception in thread "main" com.thoughtworks.selenium.SeleniumException: ERROR: Element id=skuOptionSIZE1cd7bfd not found
任何人都可以帮助我如何从下拉列表中获取值吗?
最佳答案
如果您使用 Selenium 2 又名 Webdriver,我会这样做:
Select select = new Select(driver.findElemetn(/*Way to your drop down*/));
select.selectByValue("your value")
//or
select.selectByVisibleText("your Test");
//alternativly you can do something like this
List<WebElement> options = select.getOptions();
//find your desired option
select.selectByVisibleText(option.getText());
希望有帮助。
关于java - 如何使用 selenium selenium.select ("","") 在下拉列表中选择值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9919086/