java Webdriver 下拉列表无法选择/选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6813653/
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
Webdriver Dropdown List can't select/choose
提问by KoKo
I can't select HTML Dropdown list with my Webdriver method. What was wrong in my code.? Could you give me some hints.
我无法使用我的 Webdriver 方法选择 HTML 下拉列表。我的代码有什么问题。?你能给我一些提示吗。
<select>
<option value="32">32</option>
<option value="34">34</option>
<option value="36">36</option>
</select>
public static List<WebElement> chooseSize(Integer size){
WebElement select = findElement(By.xpath(DropDown_Article_Size_XPATH_ID));
List<WebElement> options = select.findElements(By.tagName("option"));
for(WebElement option : options){
if(option.getText().equals(size)){
option.isSelected(); // or .click()?
}
}
return options;
}
采纳答案by Alberto
For such cases, I'm using xpath expressions. You'll save a lot of code!
对于这种情况,我使用的是 xpath 表达式。您将节省大量代码!
For what you are asking for, this should do (I assume that your xpath is properly targeting the corresponding select
):
对于您所要求的,应该这样做(我假设您的 xpath 正确地针对相应的select
):
// Click select first:
// (See http://code.google.com/p/selenium/issues/detail?id=2112)
findElement(By.xpath(DropDown_Article_Size_XPATH_ID)).click();
// Then click option:
String xpathOption = String.format("%s/option[text()='%d']",
DropDown_Article_Size_ID, size);
log.debug("Selecting option by text '{}' using xpath '{}'", size, xpathOption);
findElement(By.xpath(xpathOption)).click();
By the way, I don't get why your chooseSize
returns the list of all options. You should probably rename the method to something meaningful (getOptionsBySize
, for example, if this is what you want).
顺便说一句,我不明白你为什么chooseSize
返回所有选项的列表。您可能应该将该方法重命名为有意义的名称(getOptionsBySize
例如,如果这是您想要的)。
回答by Luiz Fernando Penkal
There's a support class that can help you with that in WebDriver: "org.openqa.selenium.support.ui.Select".
在 WebDriver 中有一个支持类可以帮助您解决这个问题:“org.openqa.selenium.support.ui.Select”。
Here is how you use it:
以下是您如何使用它:
// First, get the WebElement for the select tag
WebElement selectElement = driver.findElement(By.xpath(DropDown_Article_Size_XPATH_ID));
// Then instantiate the Select class with that WebElement
Select select = new Select(selectElement);
// Get a list of the options
List<WebElement> options = select.getOptions();
// For each option in the list, verify if it's the one you want and then click it
for (WebElement we : options) {
if (we.getText().equals(valueToSelect)) {
we.click();
break;
}
}
回答by dmp
Select select = new Select(driver.findElement(By.xpath("Xpath_of_Select_Element")));
select.selectByVisibleText("Option_to_Select");
This is the simplest way to select an option from a select drop down
这是从选择下拉列表中选择选项的最简单方法
回答by user2073203
Bit modification it works for me, thanks a lot such a simple code it does the job.
位修改它对我有用,非常感谢这样一个简单的代码它可以完成这项工作。
Select select = new Select(driver.findElement(By.name("Status_operator")));
select.selectByValue("=");
回答by Shrikant Khadilkar
you can do
你可以做
WebElement selectElement = driver.findElement(By.xpath(DropDown_Article_Size_XPATH_ID));
selectElement.sendKeys("34")
to select 34
选择 34
its that simple. Sendkeys is a very useful method in webdriver and has different implementations for different kind of objects i.e. for a textbox Sendkeys would type in the text, while for a select element it would select element.
就这么简单。Sendkeys 是 webdriver 中一个非常有用的方法,它对不同类型的对象有不同的实现,即对于文本框,Sendkeys 将输入文本,而对于选择元素,它将选择元素。
I have even read that for a file upload field you can do sendkeys to enter the file path.
我什至读过,对于文件上传字段,您可以使用 sendkeys 来输入文件路径。
cheers
干杯
Shrikant
什里坎特
回答by artbristol
Have you tried setSelected()
? isSelected()
is a getter so it won't change anything.
你试过setSelected()
吗?isSelected()
是一个吸气剂,所以它不会改变任何东西。
回答by Robert
If you are using Selenium2 you have to use option.click()
.
如果您使用的是 Selenium2,则必须使用option.click()
.
回答by jherranzm
I'm afraid that there's an issue with ChromeDriver and Select. Tested on Chrome for MacOSX, .click() and .isSelected() don't work. The same code in FireFox, works as expected. Is there anything different between both browsers?
我担心 ChromeDriver 和 Select 有问题。在 MacOSX 的 Chrome 上测试,.click() 和 .isSelected() 不起作用。FireFox 中的相同代码按预期工作。两种浏览器有什么不同吗?
List<WebElement> opciones = select.getOptions();
for(WebElement el : opciones){
System.out.println("Elemento disponible: ["+el.getAttribute("value")+"]["+el.getText()+"]");
//Select actual option
el.click();
if(el.isSelected())
System.out.println("Selected: ["+el.getAttribute("value")+"]["+el.getText()+"]");
}