

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to display all items in the list in the drop down in Selenium?
We can display all items in the list in the dropdown with Selenium webdriver using the Select class. A dropdown is represented by select tag and itsoptions are represented by option tag.
To obtain all the list of items we have to use the method getOptions. Its return type is list. Then we have to iterate through this list and obtain it with the help of the getText method.
Let us see the html code of a dropdown along with its options – Please select an option, Option 1 and Option 2.
Syntax
WebElement d = driver.findElement(By.tagName("select")); Select l = new Select(d); List<WebElement> m = l.getOptions();
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; import java.util.List; import org.openqa.selenium.support.ui.Select public class DrpdwnLst{ public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //URL launch driver.get("https://the-internet.herokuapp.com/dropdown"); // identify dropdown WebElement d = driver.findElement(By.tagName("select")); //Select class to get options in dropdown Select l = new Select(d); List<WebElement> m = l.getOptions(); System.out.println("Drodown list items are: "); //iterate through options till list size for (int j = 0; j < m.size(); j++) { String s = m.get(j).getText(); System.out.println(s); } driver.quit();} } }
Output
- Related Questions & Answers
- How to get all options in a drop-down list by Selenium WebDriver using C#?
- How to select/get drop down option in Selenium 2?
- How do we use a simple drop-down list of items in HTML forms?
- How to list down all the running queries in MySQL?
- How to include an option in a drop-down list in HTML?
- How to list down all the plug-in installed in your browser?
- How to select value from a drop down using Selenium IDE?
- List down the differences between Selenium and UTP.
- List down all the Tables in a MySQL Database
- How to list down all the files alphabetically using Python?
- How to select a drop-down menu option value with Selenium (Python)?
- How to list down all the files available in a directory using C#?
- How to list down all the cookies by name using JavaScript?
- List down the name of the Web drivers supported by Selenium.
- How can I get the selected value of a drop-down list with jQuery?
Advertisements