java 如何使用 selenium selenium.select("","") 在下拉列表中选择值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9919086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select values in drop down using selenium selenium.select("","")?
提问by Vinod
I am doing automation using Selenium. I am using DefaultSelenium
class, in our application I have a drop down. I want to get a value from this drop down.
我正在使用 Selenium 进行自动化。我正在使用DefaultSelenium
类,在我们的应用程序中我有一个下拉菜单。我想从这个下拉列表中获取一个值。
Initially, I have scripted with selenium IDE, it gave me the code as:
最初,我使用 selenium IDE 编写了脚本,它给了我以下代码:
selenium.select("id=skuOptionSIZE1c4b403", "label=8");
but when i start writing in code (Java), Eclipse throws an error while I am still able to see the drop down id
present on the page:
但是当我开始编写代码 (Java) 时,Eclipse 会抛出一个错误,而我仍然能够看到id
页面上的下拉菜单:
Exception in thread "main" com.thoughtworks.selenium.SeleniumException: ERROR: Element id=skuOptionSIZE1cd7bfd not found
Can any one please help me how to get the values from drop down?
任何人都可以帮助我如何从下拉列表中获取值吗?
回答by Tarken
If you are using Selenium 2 aka Webdriver I d do it like this:
如果您使用的是 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());
Hope that helps.
希望有帮助。
回答by Petr Jane?ek
If you use IE8 or greater, press F12and use the Developer Tools there. Particularly helpful should be the cursor icon, i.e. Select Element By Click which will allow you to select any element and see all the attributes assigned to it.
如果您使用 IE8 或更高版本,请按F12并使用那里的开发人员工具。特别有用的是光标图标,即通过单击选择元素,它允许您选择任何元素并查看分配给它的所有属性。
If you use Firefox 11, there is a similar tool under Web Developer menu. Or use the Firebug addon which is stronger, but more complex.
如果您使用 Firefox 11,Web Developer 菜单下有一个类似的工具。或者使用更强大但更复杂的 Firebug 插件。
But!The main problem you'll have that the id will change from time to time. It seems to be generated automatically. That means you'll have to use some other way to select the element. You can use for example selenium.select("id=skuOptionSIZE*", "label=8");
, or find it by XPathor css selector.
但!您将遇到的主要问题是 ID 会不时更改。好像是自动生成的。这意味着您必须使用其他方式来选择元素。例如selenium.select("id=skuOptionSIZE*", "label=8");
,您可以使用,或者通过XPath或css 选择器找到它。
回答by CBRRacer
If you download the Webdriver Support dll then you can use the following
如果您下载 Webdriver Support dll,则可以使用以下内容
SelectElement select = new SelectElement(element);
select.SelectByIndex(8); //Where the number 8 is the base 0 index of the options
So if you have 10 options (0-9) SelectByIndex(8)
will return the ninth option.
所以如果你有 10 个选项 (0-9)SelectByIndex(8)
将返回第九个选项。