【问题标题】:How to select an item from a dropdown list using Selenium WebDriver with java?如何使用 Selenium WebDriver 和 java 从下拉列表中选择一个项目?
【发布时间】:2012-10-08 01:57:07
【问题描述】:

如何使用 Selenium WebDriver 和 Java 从下拉列表中选择一个项目,例如性别(例如男性、女性)?

我试过了

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

我上面的代码不起作用。

【问题讨论】:

标签: java selenium webdriver selenium-webdriver


【解决方案1】:

Google “选择项目 selenium webdriver” 将How do I set an option as selected using Selenium WebDriver (selenium 2.0) client in ruby 作为第一个结果。这不是 Java,但您应该无需太多工作就可以翻译它。 https://sqa.stackexchange.com/questions/1355/what-is-the-correct-way-to-select-an-option-using-seleniums-python-webdriver 在前 5 名中,同样不是 Java,但 API 非常相似。

【讨论】:

    【解决方案2】:

    使用-

    new Select(driver.findElement(By.id("gender"))).selectByVisibleText("Germany");
    

    当然,你需要import org.openqa.selenium.support.ui.Select;

    【讨论】:

    • 请注意,在 C# 中,该类是 SelectElement 而不是 Select。而且它不是核心Selenium.WebDriver 包的一部分,要使用这个类,你还必须安装Selenium.Support 包。
    • 您能告诉我们为什么需要将 WebElement 转换为 Select 实例吗?仅仅是为了让我们可以获得 Select 特定的方法,从而可以轻松地自动化 select 吗?另外,我们可以在不使用 Select 类的情况下自动进行选择吗?
    【解决方案3】:
    WebElement selectgender = driver.findElement(By.id("gender"));
    selectgender.sendKeys("Male");
    

    【讨论】:

      【解决方案4】:

      您可以使用 Maitreya 发布的 Selenium WebDriver 的“选择”类。抱歉,但我有点困惑,从下拉菜单中选择性别,为什么要比较字符串与“德国”。这里是代码sn-p,

      Select gender = new Select(driver.findElement(By.id("gender")));
      gender.selectByVisibleText("Male/Female");
      

      添加上述代码后导入import org.openqa.selenium.support.ui.Select;。 现在将选择您给过的性别(男/女)。

      【讨论】:

        【解决方案5】:
        public class checkBoxSel {
        
            public static void main(String[] args) {
        
                 WebDriver driver = new FirefoxDriver();
                 EventFiringWebDriver dr = null ;
        
        
                 dr = new EventFiringWebDriver(driver);
                 dr.get("http://www.google.co.in/");
        
                 dr.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        
                 dr.findElement(By.linkText("Gmail")).click() ;
        
                 Select sel = new Select(driver.findElement(By.tagName("select")));
                 sel.selectByValue("fil");
        
            }
        
        }
        

        我正在使用 GOOGLE LOGIN PAGE 来测试选择选项。上面的示例是从下拉列表中查找并选择语言“菲律宾语”。我相信这会解决问题。

        【讨论】:

          【解决方案6】:
          WebElement select = driver.findElement(By.id("gender"));
          List<WebElement> options = select.findElements(By.tagName("option"));
          for (WebElement option : options) {
             if("Germany".equals(option.getText()))
                 option.click();   
          }
          

          【讨论】:

            【解决方案7】:

            只需将您的 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");
            

            快乐编码:)

            【讨论】:

              【解决方案8】:

              你应该提到像“选项”这样的标记名,如果文本带有空格,我们可以使用这种方法它应该可以工作。

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

              【讨论】:

                【解决方案9】:

                要查找特定的下拉框元素:

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

                获取下拉框中包含的所有元素的列表:

                for(int j=1;j<3;j++)
                    System.out.println(gender.getOptions().get(j).getText());
                

                通过单击它时显示的可见文本来选择它:

                gender.selectByVisibleText("Male");
                

                按索引选择它(从 0 开始):

                gender.selectByIndex(1);
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-07-13
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多