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

前端 未结 11 2077
遥遥无期
遥遥无期 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 allOptions = select.getOptions();
    

    Now you can do what you want with allOptions.

提交回复
热议问题