简体   繁体   English

我怎样才能 select 下拉菜单中的所有选项 - Selenium Webdriver?

[英]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.我知道那是一年前,但它仍然可以帮助某人。

  1. First check if the drop down supports multiple selection.首先检查下拉列表是否支持多选。
  2. If multiple selections is possible, collect all the options of the select into a list.如果可以进行多项选择,则将选择的所有选项收集到一个列表中。
  3. 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.

相关问题 在月份下拉列表中选择值-Selenium Webdriver - Select values in the month drop down- Selenium Webdriver 如何比较下拉选项与Selenium WebDriver中的UI选项是否匹配? - How to compare the drop down options is matching with the UI options in Selenium WebDriver? 如何从 Selenium WebDriver 的下拉列表中获取所有元素? - How can I get all elements from drop down list in Selenium WebDriver? 如何使用Java计算Selenium WebDriver中选择下拉框中的选项数量? - How to count the number of options in a select drop down box in Selenium WebDriver using Java? 如何使用 Selenium WebDriver java 从下拉列表中获取所有选项 - How to get all the options from drop down using Selenium WebDriver java 按钮ng下拉菜单-如何在Java中使用WebDriver选择项目 - Button ng drop down- how to choose the item using webdriver with java 使用Java的Selenium Webdriver:如何从下拉选择选项菜单中选择随机的出生月份和随机的国家 - Selenium Webdriver with Java: How to select a random birth month and a random country from drop-down select options menu Selenium webdriver 2.0:从下拉列表中选择下一个用户,然后依次单击Login \\ logout。 如何在Java中循环播放 - Selenium webdriver 2.0 : select next user from drop down list and Login\logout in sequence. How can I Loop it in Java 如何选择jquery无序下拉列表selenium webdriver - how to select jquery unorderded drop down list selenium webdriver 如何从下拉列表循环中选择所有列表选项,选择每个选项,然后使用Selenium Webdriver单击Submit按钮 - How to select all list options from drop downlist loop through them selcet each option and click submit button using selenium webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM