【问题标题】:How can I select all the options in a drop down- Selenium Webdriver?如何选择下拉菜单中的所有选项 - Selenium Webdriver?
【发布时间】:2014-02-26 16:38:43
【问题描述】:
  • 目前正在开发 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>
    

等等..

这里是示例代码:

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");

【问题讨论】:

  • 描述和底线问题非常不清楚
  • 如果我想从下拉列表中选择多个选项,我将取消选择所有选项,我正在选择我想要的选项.. 再次我想要所有选项我如何取消选择.. 选择.deselectAll();这种方法。
  • 对不起;仍然非常不清楚。您可以在问题中添加一些代码吗?

标签: java html selenium selenium-webdriver


【解决方案1】:
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

【讨论】:

  • 尝试更清楚地解释为什么这是问题的答案
【解决方案2】:
  1. 首先检查下拉菜单是否支持多选。
  2. 如果 多选是可能的,收集所有选项 选择到列表中。
  3. 使用 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
    }
    

【讨论】:

    【解决方案3】:

    我建议尝试另一种解决方案,之前我也使用循环来选择下拉列表中的所有元素,但是当它们的数量很大时,可能需要很长时间。我尝试过并且成功的是:

    element(By.id("dropdownId")).selectByIndex(0);
    element(By.id("dropdownId")).sendKeys(Keys.SHIFT, Keys.END);
    

    我知道那是一年前的事了,但它仍然可以帮助某人。

    【讨论】:

      【解决方案4】:

      我们获得了 web 元素列表的所有选项。然后我们可以遍历这个列表来选择所有的选项。

      Select select31 = new Select(driver.findElement(By.id("ProductId")));
      select31.deselectAll();
      
      List<WebElement> select31Options = select31.getOptions();
      
      for (WebElement option : select31Options) {
          select31.selectByVisibleText(option.getText());
      }
      

      如果这对你有帮助,请告诉我。

      【讨论】:

        【解决方案5】:

        您不能使用类似于 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());
        }
        

        看看它是否有帮助。对于 Control + A,请尝试以下操作:

        Actions builder = new Actions(driver);
        builder.sendKeys(Keys.chord(Keys.CONTROL,"a")).perform();
        

        【讨论】:

        • 上述HTML标签有没有其他方法可以通过Ctrl + A同时选择所有选项
        • 我不知道它对 Select 有什么作用,但你可以试试上面的 Ctrl+A 代码。如果您需要通过它选择复选框,您甚至可以在构建器中添加空格键。元素的 HTML 可以更好地帮助您,此外,当有其他可用方式时,为什么要使用 ctrl+a?
        猜你喜欢
        • 1970-01-01
        • 2017-10-29
        • 1970-01-01
        • 1970-01-01
        • 2016-10-10
        • 2023-04-02
        • 1970-01-01
        • 2018-02-04
        相关资源
        最近更新 更多