[英]How can I select all the options in a drop down- Selenium Webdriver?
Currently working on Selenium WebDriver and using Java .目前正在研究Selenium WebDriver并使用Java 。 If I have drop down name called Product ..
如果我有名为Product的下拉名称..
In that drop down I have so many values (for ex:60).在那个下拉列表中,我有很多值(例如: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..
执行代码时,我取消选择所有选项,然后我选择了我想要的选项,因为默认情况下,所有值都在 HTML 中选择..并且工作正常..
In the same way if I want to select all options at the same time.. How can I perform the action.同样,如果我想同时 select 所有选项.. 我该如何执行该操作。
<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");
You can not use anything similar to deselectAll().您不能使用类似于 deselectAll() 的任何内容。 However you can iterate through each option and select each time.
但是,您可以遍历每个选项并每次都选择。 Try following:
尝试以下操作:
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();
We get all the options to a list of webelements.我们获得了 webelement 列表的所有选项。 Then we can iterate through this list to select all the options.
然后我们可以遍历此列表以选择所有选项。
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.如果这对您有帮助,请告诉我。
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.我知道那是一年前,但它仍然可以帮助某人。
Use a for loop to iterate over all the elements in the list and select them.使用 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 }
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
for (WebElement webElement : options) {
webElement.click();
System.out.println(webElement.getText());
}
Inside the Enhanced for loop, added webElement.click()
.在增强的 for 循环内,添加了
webElement.click()
。 This selects the web element.这将选择 web 元素。 As this loop does not have any condition on it, all the values are selected.
由于此循环没有任何条件,因此选择了所有值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.