How can I get all elements from drop down list in Selenium WebDriver?

前端 未结 11 2050
遥遥无期
遥遥无期 2020-12-11 06:10

How can I get all elements from a drop down list? I used the code below:

List elements = driver.findElements(By.id(\"s\"));
相关标签:
11条回答
  • 2020-12-11 06:43

    There is a class designed for this in the bindigs.

    You are looking for the Select class:

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

    You would need to 'find' the actual select element, not the individual options. Find that select element, and let Selenium & the Select class do the rest of the work for you.

    You'd be looking for something like (s being the actual select element):

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

    The Select class has a handy getOptions() method. This will do exactly what you think it does.

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

    Now you can do what you want with allOptions.

    0 讨论(0)
  • 2020-12-11 06:45
    List<WebElement> featureList;
    
    featureList = Locate your Element;
    
    for (WebElement i : featureList) {              
    System.out.println("\n**********************  " + i.getText());
    }
    
    0 讨论(0)
  • 2020-12-11 06:47

    Use this hope it will helpful to you.

    List WebElement allSuggestions = driver.findElements(By.xpath("Your Xpath"));      
            for (WebElement suggestion : allSuggestions)
         {
            System.out.println(suggestion.getText());
    
            }
    
    0 讨论(0)
  • 2020-12-11 06:50

    This will help to list all the elements from the dropdown:

        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());
    
        }
    
    0 讨论(0)
  • 2020-12-11 06:50
    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());
    
    }
    
    0 讨论(0)
提交回复
热议问题