【问题标题】:Selenium WebDriver and DropDown BoxesSelenium WebDriver 和下拉框
【发布时间】:2011-11-06 03:33:02
【问题描述】:

如果我想选择下拉框的选项,有几种方法可以做到这一点。我一直用:

driver.findElement(By.id("selection")).sendKeys("Germany");

但这并不是每次都奏效。有时会选择另一个选项。所以我google了一下,发现这段代码每次都有效:

WebElement select = driver.findElement(By.id("selection"));
    List<WebElement> options = select.findElements(By.tagName("option"));
    for (WebElement option : options) {
        if("Germany".equals(option.getText()))
            option.click();
    }

但这真的很慢。如果我的清单很长,里面有很多项目,那真的需要太多时间。所以我的问题是,有没有一种解决方案每次都有效且速度很快?

【问题讨论】:

    标签: java selenium drop-down-menu selenium-webdriver webdriver


    【解决方案1】:

    你可以试试这个:

    IWebElement dropDownListBox = driver.findElement(By.Id("selection"));
    SelectElement clickThis = new SelectElement(dropDownListBox);
    clickThis.SelectByText("Germany");
    

    【讨论】:

    • 我认为这是一些 C# 代码之类的?但它帮助我弄清楚了以下代码: WebElement dropDownListBox = driver.findElement(By.id("selection"));选择 clickThis = new Select(dropDownListBox); clickThis.selectByValue("德国");快很多!谢谢!
    • IWebElement 和 SelectElement 应该导入哪个包?
    • 在哪里可以找到 SelectElement 类?
    • C# 中的注释SelectElement 是名为 Selenium.Support 的不同 NuGet 项目的一部分。
    【解决方案2】:

    尝试以下方法:

    import org.openqa.selenium.support.ui.Select;
    
    Select droplist = new Select(driver.findElement(By.Id("selection")));   
    droplist.selectByVisibleText("Germany");
    

    【讨论】:

    • 它的scala实现是什么?谢谢
    【解决方案3】:

    试试 Select 辅助类,看看有什么不同?

    String valueToSelect= "Germany";
    WebElement select = driver.findElement(By.id("selection"));
    Select dropDown = new Select(select);           
    String selected = dropDown.getFirstSelectedOption().getText();
    if(selected.equals(valueToSelect)) {//do stuff already selected}
    List<WebElement> Options = dropDown.getOptions();
    for(WebElement option:Options){
      if(option.getText().equals(valueToSelect)){
           option.click();  
      }
    }
    

    【讨论】:

    • sry,这和我的解决方案一样慢。
    【解决方案4】:

    由于某些奇怪的原因,webdriver(版本 2.25.1.0)的SelectElement 不能与 firefoxdriver(Firefox 15)一起正常工作。有时它可能不会从下拉列表中选择一个选项。但是,它似乎可以与 chromedriver 一起使用...This 是指向 chromedriver 的链接...只需将其放在 bin 目录中即可。

    【讨论】:

      【解决方案5】:

      从下拉列表中选择选项的示例:

      使用 id 或 csspath 或 xpath 或名称单击下拉列表。我这里用过id。

      driver.findElement(By.id("dropdownlistone")).click(); // To click on drop down list
      driver.findElement(By.linkText("india")).click(); // To select a data from the drop down list.
      

      【讨论】:

        【解决方案6】:

        只需将您的 WebElement 包装到 Select Object 中,如下所示

        Select dropdown = new Select(driver.findElement(By.id("identifier")));
        

        完成后,您可以通过 3 种方式选择所需的值。考虑一个这样的 HTML 文件

        <html>
        <body>
        <select id = "designation">
        <option value = "MD">MD</option>
        <option value = "prog"> Programmer </option>
        <option value = "CEO"> CEO </option>
        </option>
        </select>
        <body>
        </html>
        

        现在来识别下拉菜单

        Select dropdown = new Select(driver.findElement(By.id("designation")));
        

        要选择它的选项说“程序员”,你可以这样做

        dropdown.selectByVisibleText("Programmer ");
        

         dropdown.selectByIndex(1);
        

         dropdown.selectByValue("prog");
        

        快乐编码:)

        【讨论】:

          【解决方案7】:

          我必须努力寻找如何实现,尤其是那些不熟悉这个工具的人(比如我)

          C#代码:

          IWebElement ddl = ffDriver.FindElement(By.Id("ddlGoTo")); 
          OpenQA.Selenium.Support.UI.SelectElement clickthis = new OpenQA.Selenium.Support.UI.SelectElement(ddl);
          clickthis.SelectByText("Your Text");
          

          希望这对其他人有所帮助!

          【讨论】:

            【解决方案8】:
            public static void mulptiTransfer(WebDriver driver, By dropdownID, String text, By to)
            {   
                String valuetext = null;
                WebElement element = locateElement(driver, dropdownID, 10);
                Select select = new Select(element);
                List<WebElement> options = element.findElements(By.tagName("option"));
                for (WebElement value: options) 
                {
                    valuetext = value.getText();
                    if (valuetext.equalsIgnoreCase(text))
                    {
                        try
                        {
                            select.selectByVisibleText(valuetext);
                            locateElement(driver, to, 5).click();                           
                            break;
                        }
                        catch (Exception e)
                        {
                            System.out.println(valuetext + "Value not found in Dropdown to Select");
                        }       
                    }
                }
            }
            

            【讨论】:

              【解决方案9】:
              select = driver.FindElement(By.CssSelector("select[uniq id']"));
                              selectElement = new SelectElement(select);
                              var optionList =
                                  driver.FindElements(By.CssSelector("select[uniq id']>option"));
                              selectElement.SelectByText(optionList[GenerateRandomNumber(1, optionList.Count())].Text);
              

              【讨论】:

                【解决方案10】:

                你可以用这个

                (new SelectElement(driver.FindElement(By.Id(""))).SelectByText("");
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-05-29
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-11-04
                  • 1970-01-01
                  相关资源
                  最近更新 更多