【问题标题】:How can I get all elements from drop down list in Selenium WebDriver?如何从 Selenium WebDriver 的下拉列表中获取所有元素?
【发布时间】:2013-05-22 00:33:50
【问题描述】:

如何从下拉列表中获取所有元素?我使用了下面的代码:

List<WebElement> elements = driver.findElements(By.id("s"));

但我总是只得到第一个元素。

【问题讨论】:

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


【解决方案1】:

娜美塔,

使用以下代码,您将在选择框中获得可用选项列表,然后单击其中一个。

列表选项 = select.findElements(By.tagName("option"));

    for(WebElement option : options){
        if(option.getText().equals("male")) {
            option.click();
            break;
        }
    }

【讨论】:

    【解决方案2】:

    在绑定中有一个为此设计的类。

    您正在寻找Select 类:

    https://code.google.com/p/selenium/source/browse/java/client/src/org/openqa/selenium/support/ui/Select.java

    您需要“找到”实际的select 元素,而不是单个选项。找到 select 元素,然后让 Selenium 和 Select 类为您完成剩下的工作。

    你会寻找类似的东西(s 是实际的select 元素):

    WebElement selectElement = driver.findElement(By.id("s");
    Select select = new Select(selectElement);
    

    Select 类有一个方便的 getOptions() 方法。这将完全按照您的想法进行。

    List<WebElement> allOptions = select.getOptions();
    

    现在您可以使用allOptions 为所欲为。

    【讨论】:

    • 这很好。谢谢你。有没有办法获取每个选项的值?
    【解决方案3】:

    使用这个希望对你有帮助。

    List WebElement allSuggestions = driver.findElements(By.xpath("Your Xpath"));      
            for (WebElement suggestion : allSuggestions)
         {
            System.out.println(suggestion.getText());
    
            }
    

    【讨论】:

      【解决方案4】:

      这将有助于列出下拉列表中的所有元素:

          Select dropdown = new Select(driver.findElement(By.id("id")));
      
          //Get all options
          List<WebElement> dd = dropdown.getOptions();
      
          //Get the length
          System.out.println(dd.size());
      
          // Loop to print one by one
          for (int j = 0; j < dd.size(); j++) {
              System.out.println(dd.get(j).getText());
      
          }
      

      【讨论】:

        【解决方案5】:
        List<WebElement> featureList;
        
        featureList = Locate your Element;
        
        for (WebElement i : featureList) {              
        System.out.println("\n**********************  " + i.getText());
        }
        

        【讨论】:

        • OP 为什么要试试这个好的答案将始终解释所做的事情以及这样做的原因,不仅适用于 OP,而且适用于可能会发现此问题并正在阅读您的答案的 SO 的未来访问者。跨度>
        【解决方案6】:
        WebElement ele=driver.findElement(By.xpath(".//*[@id='month']"));
                List<WebElement> x = ele.findElements(By.tagName("option"));
                for(WebElement ele1:x) {
                    String y=ele1.getAttribute("innerHTML");
                    System.out.println(y);
                }
        

        【讨论】:

        • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
        【解决方案7】:
        String [] ex = {"A","b","c","d"};
        
        List<WebElement> Str  = driver.findElements(By.xpath("//*[@id=\"institutionName\"]/option"));
        
        for (int i = 0; i < ex.length; i++) {
            System.out.println(Str.get(i).getText());  
            Assert.assertEquals(ex[i], Str.get(i).getText());
        }
        

        【讨论】:

          【解决方案8】:
          Select drop = new Select(driver.findElement(By.id("id")));
          
          List<WebElement> dd = drop.getOptions();
          
          System.out.println(dd.size());
          
          for (int j = 0; j < dd.size(); j++) {
              System.out.println(dd.get(j).getText());
          
          }
          

          【讨论】:

          • 请在你的答案周围添加一些细节。
          • 有什么说明吗?
          【解决方案9】:

          这是一种获取所有下拉列表并返回列表的方法。

              public List getDropDownList(String Xpath) {
              WebElement dropdowns = driver.findElement(By.xpath(Xpath));
              Select select = new Select(dropdowns);
              List<String> dropdown = new ArrayList<String>();
              List<WebElement> allOptions = select.getAllSelectedOptions();
              Iterator<WebElement> itr = allOptions.iterator();
              while (itr.hasNext()) {
                  String st = itr.next().getText();
                  dropdown.add(st);
               }
              return dropdown;
             }
          

          希望对你有所帮助。

          【讨论】:

            【解决方案10】:

            我以 Facebook 的注册页面为例,可能有助于理解。

            这是从月份下拉列表中获取所有月份名称选项的代码。

            List<WebElement> option = driver.findElements((By.xpath("//[@id='month']/option")));
            
            ArrayList a = new ArrayList();
            
                    for (WebElement str: option)
                    {
                       String s =  str.getText();
                       if(!s.equals("Month")) {
                           a.add(s);
                     }
                       else {
                           continue;
                       }
                    }
            
            System.out.println("The Output is: "+ a);
            

            上面代码的解释是,

            1. 将所有元素存储在一个列表中。
            2. 声明一个空的数组列表来存储选项。
            3. 通过使用for每个循环将所有选项一一提取并存储到ArrayList中。
            4. 将所有选项打印为列表。

            希望这会对你有所帮助。! 祝你好运!!

            【讨论】:

              【解决方案11】:

              有多种方法可以从option 元素打印文本。理想情况下,在与 元素交互时,您需要使用 Select 类。进一步与&lt;option&gt;标签进行交互,您需要使用getOptions()方法。


              演示

              例如打印来自 DayMonthYear 元素的文本 option 登陆页面 @987654338 @你需要诱导WebDriverWaitelementToBeClickable(),可以使用下面的Locator Strategies


              Day 下拉菜单中的选项

              使用id属性:

              • 代码块:

                WebElement dayElement = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("day")));
                Select selectDay = new Select(dayElement);
                List<WebElement> dayList = selectDay.getOptions();
                for (int i=0; i<dayList.size(); i++)
                    System.out.println(dayList.get(i).getText());
                

              月份下拉菜单中的选项

              使用 stream()map()

              • 代码块:

                Select selectMonth = new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[@id='month']"))));
                List<String> myMonths = selectMonth.getOptions().stream().map(element->element.getText()).collect(Collectors.toList());
                System.out.println(myMonths);
                
              • 控制台输出:

                [Month, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Nov, Dec]
                

              使用单行代码从年份下拉菜单中选择选项

              在一行代码中使用 [tag:css_selectors] 和 stream()map()

              • 代码行:

                System.out.println(new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select#year")))).getOptions().stream().map(element->element.getText()).collect(Collectors.toList()));
                
              • 控制台输出:

                [Year, 2020, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000, 1999, 1998, 1997, 1996, 1995, 1994, 1993, 1992, 1991, 1990, 1989, 1988, 1987, 1986, 1985, 1984, 1983, 1982, 1981, 1980, 1979, 1978, 1977, 1976, 1975, 1974, 1973, 1972, 1971, 1970, 1969, 1968, 1967, 1966, 1965, 1964, 1963, 1962, 1961, 1960, 1959, 1958, 1957, 1956, 1955, 1954, 1953, 1952, 1951, 1950, 1949, 1948, 1947, 1946, 1945, 1944, 1943, 1942, 1941, 1940, 1939, 1938, 1937, 1936, 1935, 1934, 1933, 1932, 1931, 1930, 1929, 1928, 1927, 1926, 1925, 1924, 1923, 1922, 1921, 1920, 1919, 1918, 1917, 1916, 1915, 1914, 1913, 1912, 1911, 1910, 1909, 1908, 1907, 1906, 1905]
                

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-08-12
                相关资源
                最近更新 更多