Java 如何在下拉列表中选择所有选项 - Selenium Webdriver?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21523199/
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 can I select all the options in a drop down- Selenium Webdriver?
提问by testing
Currently working on Selenium WebDriverand using Java. If I have drop down name called Product..
In that drop down I have so many values (for ex:60). when executing the code I deselect all option then I selected which option I want because by default all values are selected in the HTML.. and it is working fine..
In the same way if I want to select all options at the same time.. How can I perform the action.
<select id="productId" multiple="" style="width: 125px; display: none;" name="products[]"> <option selected="" value="1020 ROUTER SERIES">1020 ROUTER SERIES</option> <option selected="" value="1030 ROUTER SERIES">1030 ROUTER SERIES</option> <option selected="" value="1040 ROUTER SERIES">1040 ROUTER SERIES</option> <option selected="" value="1061 ROUTER">1061 ROUTER</option> </select>
目前正在研究Selenium WebDriver并使用Java。如果我有名为Product 的下拉名称..
在那个下拉列表中,我有很多值(例如:60)。执行代码时,我取消选择所有选项,然后我选择了我想要的选项,因为默认情况下,所有值都在 HTML 中选择。而且它工作正常。
以同样的方式,如果我想同时选择所有选项.. 我该如何执行操作。
<select id="productId" multiple="" style="width: 125px; display: none;" name="products[]"> <option selected="" value="1020 ROUTER SERIES">1020 ROUTER SERIES</option> <option selected="" value="1030 ROUTER SERIES">1030 ROUTER SERIES</option> <option selected="" value="1040 ROUTER SERIES">1040 ROUTER SERIES</option> <option selected="" value="1061 ROUTER">1061 ROUTER</option> </select>
and so on..
等等..
Here is the sample code:
这是示例代码:
Log.info("Clicking on Product dropdown");
JavascriptExecutor executor31 = (JavascriptExecutor)driver;
executor31.executeScript("document.getElementById('ProductId').style.display='block';");
Select select31 = new Select(driver.findElement(By.id("ProductId")));
select31.deselectAll();
select31.selectByVisibleText("1222");
Thread.sleep(6000);
JavascriptExecutor executor32 = (JavascriptExecutor)driver;
executor32.executeScript("document.getElementById('ProductId').style.display='block';");
Select select32 = new Select(driver.findElement(By.id("ProductId")));
select32.selectByVisibleText("1020");
回答by Husam
You can not use anything similar to deselectAll(). However you can iterate through each option and select each time. Try following:
您不能使用类似于 deselectAll() 的任何内容。但是,您可以遍历每个选项并每次都选择。尝试以下操作:
List<WebElement> liOp = new Select(driver.findElement(By.id("YourLocator"))).getOptions();
for(WebElement eachElem:liOp){
new Select(driver.findElement(By.id("yourLocator"))).selectByVisibleText(eachElem.getText());
}
See whether it helps. For Control + A, try following:
看看有没有帮助。对于 Control + A,请尝试以下操作:
Actions builder = new Actions(driver);
builder.sendKeys(Keys.chord(Keys.CONTROL,"a")).perform();
回答by Sighil
We get all the options to a list of webelements. Then we can iterate through this list to select all the options.
我们获得了 webelement 列表的所有选项。然后我们可以遍历此列表以选择所有选项。
Select select31 = new Select(driver.findElement(By.id("ProductId")));
select31.deselectAll();
List<WebElement> select31Options = select31.getOptions();
for (WebElement option : select31Options) {
select31.selectByVisibleText(option.getText());
}
Let me know if this helps you.
如果这对您有帮助,请告诉我。
回答by rusk4life
I suggest trying another solution, earlier I was using loop as well to select all elements in dropdown, but when their number is big it can take really long. What I tried and it worked was:
我建议尝试另一种解决方案,之前我也使用循环来选择下拉列表中的所有元素,但是当它们的数量很大时,它可能需要很长时间。我尝试过并且有效的是:
element(By.id("dropdownId")).selectByIndex(0);
element(By.id("dropdownId")).sendKeys(Keys.SHIFT, Keys.END);
I know it was a year ago but still it can help someone.
我知道那是一年前,但它仍然可以帮助某人。
回答by Revanth Kumar
- First check if the drop down supports multiple selection.
- If multiple selections is possible, collect all the options of the select into a list.
Use a for loop to iterate over all the elements in the list and select them.
Select selectElement = new Select(driver.findElement(By.Id("productId"))); if (selectElement.isMultiple()) { /* step 1 */ List<WebElement> options = selectElement.getOptions(); /* step 2 */ for (WebElement we : options) { /* step 3 */ we.selectByVisibleText(we.getText()); } } else { // does not support multiple }
- 首先检查下拉列表是否支持多选。
- 如果可以进行多项选择,则将选择的所有选项收集到一个列表中。
使用 for 循环遍历列表中的所有元素并选择它们。
Select selectElement = new Select(driver.findElement(By.Id("productId"))); if (selectElement.isMultiple()) { /* step 1 */ List<WebElement> options = selectElement.getOptions(); /* step 2 */ for (WebElement we : options) { /* step 3 */ we.selectByVisibleText(we.getText()); } } else { // does not support multiple }
回答by Jayesh Nagdeote
driver.get("https://www.w3schools.com/tags/tryit.asp?
filename=tryhtml_select_multiple");
driver.manage().window().maximize();
driver.switchTo().frame("iframeResult");
WebElement ele = driver.findElement(By.name("cars")); // Get control of select tag
Select select = new Select(ele);
List<WebElement> allOptions = select.getOptions();
ele.sendKeys(Keys.CONTROL); // to hold CTRL button once and then click on all options
for (WebElement webElement : allOptions) {
webElement.click();
}
Thread.sleep(5000);
select.deselectAll(); // to deselect all values