【问题标题】:How to select/get drop down option in Selenium 2如何在 Selenium 2 中选择/获取下拉选项
【发布时间】:2011-09-19 19:16:04
【问题描述】:

我正在将我的 selenium 1 代码转换为 selenium 2,但找不到任何简单的方法来在下拉菜单中选择标签或获取下拉菜单的选定值。你知道如何在 Selenium 2 中做到这一点吗?

以下是在 Selenium 1 中有效但在 2 中无效的两个语句:

browser.select("//path_to_drop_down", "Value1");
browser.getSelectedValue("//path_to_drop_down");

【问题讨论】:

  • 您是否尝试过使用 Firebug 找到它?使用 Firebug/xpather 生成的 xpath 会更容易。
  • 问题不在于定位或查找下拉菜单。它是关于在该下拉列表中选择一个标签。我可以找到下拉菜单,但不知道在 Selenium 2 中调用哪个方法,因为 select() 和 getSelectedValue() 或 getSelectedLabel() 在 Selenium 2 中不起作用。

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


【解决方案1】:

查看 selenium 文档中关于 filling in forms 使用 webdriver 的部分以及 Select 类的 javadoc。

根据标签选择选项:

Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));
select.deselectAll();
select.selectByVisibleText("Value1");

获取第一个选择的值:

WebElement option = select.getFirstSelectedOption()

【讨论】:

  • By.xpath("//path_to_drop_down")。我会用像 By.name 这样的定位器替换它。
  • 如果选择不支持多选,deselectAll 将抛出 UnsupportedOperationException
  • 在 C# 中,使用 SelectElement 类,所以:SelectElement salesExecutiveDropDown = new SelectElement(webDriver.FindElement(By.Id("salesExecutiveId")));
  • 仅供参考,在我注释掉这一行之前,此代码无法选择下拉菜单: //select.deselectAll();然后它开始工作。您的里程可能会有所不同。
  • 注意deselectAll只对多选有效:selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/…
【解决方案2】:
driver.findElement(By.id("id_dropdown_menu")).click();
driver.findElement(By.xpath("xpath_from_seleniumIDE")).click();

【讨论】:

    【解决方案3】:

    在 ruby​​ 中不断使用,添加如下:

    module Selenium
      module WebDriver
        class Element
          def select(value)
            self.find_elements(:tag_name => "option").find do |option|
              if option.text == value
                option.click
                  return
               end
           end
        end
      end
    end
    

    您将能够选择值:

    browser.find_element(:xpath, ".//xpath").select("Value")
    

    【讨论】:

      【解决方案4】:

      尝试使用:

      selenium.select("id=items","label=engineering")
      

      selenium.select("id=items","index=3")
      

      【讨论】:

        【解决方案5】:

        与 janderson 上面发布的内容类似的选项就是使用 selenium 2 中的 .GetAttribute 方法。使用此方法,您可以获取任何具有特定值或标签的项目。这可用于确定元素是否具有标签、样式、值等。执行此操作的常用方法是遍历下拉列表中的项目,直到找到所需的项目并选择它。在 C# 中

        int items = driver.FindElement(By.XPath("//path_to_drop_Down")).Count(); 
        for(int i = 1; i <= items; i++)
        {
            string value = driver.FindElement(By.XPath("//path_to_drop_Down/option["+i+"]")).GetAttribute("Value1");
            if(value.Conatains("Label_I_am_Looking_for"))
            {
                driver.FindElement(By.XPath("//path_to_drop_Down/option["+i+"]")).Click(); 
                //Clicked on the index of the that has your label / value
            }
        }
        

        【讨论】:

          【解决方案6】:

          你可以这样做:

          public void selectDropDownValue(String ValueToSelect) 
          {
          
              webelement findDropDownValue=driver.findElements(By.id("id1"))    //this will find that dropdown 
          
              wait.until(ExpectedConditions.visibilityOf(findDropDownValue));    // wait till that dropdown appear
          
              super.highlightElement(findDropDownValue);   // highlight that dropdown     
          
              new Select(findDropDownValue).selectByValue(ValueToSelect);    //select that option which u had passed as argument
          }
          

          【讨论】:

            【解决方案7】:

            此方法将返回下拉列表的选定值,

            public static String getSelected_visibleText(WebDriver driver, String elementType, String value)
              {
                WebElement element = Webelement_Finder.webElement_Finder(driver, elementType, value);
               Select Selector = new Select(element);
                Selector.getFirstSelectedOption();
                String textval=Selector.getFirstSelectedOption().getText();
                return textval;
              }
            

            同时

            String textval=Selector.getFirstSelectedOption();

            element.getText();

            将返回下拉列表中的所有元素。

            【讨论】:

              【解决方案8】:

              在 Selenium WebDriver 中选择

              Selenium WebDriver 中的“Select”类用于选择和取消选择下拉菜单中的选项。 Select 类型的对象可以通过将下拉 webElement 作为参数传递给其构造函数来初始化。

              WebElement testDropDown = driver.findElement(By.id("testingDropdown")); Select dropdown = new Select(testDropDown);

              从下拉列表中选择选项

              从下拉列表中选择选项有三种方式-

              1. selectByIndex – 根据索引选择选项,从 0 开始。

              dropdown.selectByIndex(3);

              1. selectByValue – 根据其“值”属性选择选项。

              dropdown.selectByValue("数据库");

              1. selectByVisibleText – 根据选项上方的文本选择选项。

              dropdown.selectByVisibleText("数据库测试");

              【讨论】:

                【解决方案9】:

                这是从下拉列表中选择值的代码

                selectlocator 的值将是 xpath 或下拉框的名称,而 optionLocator 将具有从下拉框中选择的值。

                public static boolean select(final String selectLocator,
                        final String optionLocator) {
                    try {
                        element(selectLocator).clear();
                        element(selectLocator).sendKeys(Keys.PAGE_UP);
                        for (int k = 0; k <= new Select(element(selectLocator))
                                .getOptions().size() - 1; k++) {
                            combo1.add(element(selectLocator).getValue());
                            element(selectLocator).sendKeys(Keys.ARROW_DOWN);
                        }
                        if (combo1.contains(optionLocator)) {
                            element(selectLocator).clear();
                            new Select(element(selectLocator)).selectByValue(optionLocator);
                            combocheck = element(selectLocator).getValue();
                            combo = "";
                
                            return true;
                        } else {
                            element(selectLocator).clear();
                            combo = "The Value " + optionLocator
                                    + " Does Not Exist In The Combobox";
                            return false;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        errorcontrol.add(e.getMessage());
                        return false;
                    }
                }
                
                
                
                private static RenderedWebElement element(final String locator) {
                    try {
                
                        return (RenderedWebElement) drivers.findElement(by(locator));
                    } catch (Exception e) {
                        errorcontrol.add(e.getMessage());
                        return (RenderedWebElement) drivers.findElement(by(locator));
                    }
                }
                

                谢谢,

                雷卡。

                【讨论】:

                • -1 方式过于复杂并使用不推荐使用的方法(RenderedWebElement)
                猜你喜欢
                • 1970-01-01
                • 2012-01-29
                • 2015-02-01
                • 2019-11-11
                • 2019-05-28
                • 1970-01-01
                • 1970-01-01
                • 2018-02-17
                • 1970-01-01
                相关资源
                最近更新 更多