How to select options from dropdown in Selenium WebDriver?

Category: Selenium WebDriver 291 0

Hola Learners, Hope you are doing well with your Selenium learning. Today we are going to discuss handling dropdown in Selenium WebDriver using Select class. The dropdown is just like another WebElements, though it is packed and hidden within. We can use various programming logic and sometimes even action class to handle dropdown in Selenium, hence, in such case, our simple test case will become quite complicated. Therefore, we should follow a kind of approach where our code should not fall while at runtime. Also, it should look neat and clean and have the possibility of further improvements as well as a modification; if any another test automation engineer wishes to do so.

 

How to proceed with the scenario to handle dropdown in Selenium WebDriver?

Most of the UI developers take the liability to use HTML’s Select tag to create a list of dropdown elements. If they are following this process of implementing dropdown list then we can easily handle such dropdown by instantiating Select in Selenium. Here is the Select package in Selenium:

Thus, if you are using Select command to handle dropdown in Selenium WebDriver then you should make sure that you are importing the above package in your test automation program.

dropdown in selenium

Inspected code structure of Select tag

 

 

So this is the sample structure of dropdown code in HTML. If any dropdown lists come in given structure then we can proceed with the technique discussed over here, else, we need to frame different technique.

Hot Tutorials:

Dynamix XPath Tutorial

Dynamic CSS Selector Tutorial

Robot Class Tutorial

Handle Radio button & Checkbox button in Selenium WebDriver

How to start with Select package in Selenium to handle dropdown

Once you identified the Select box and inspected like above HTML code then it’s time to instantiate select in Selenium WebDriver.

Use of the methods to select element from dropdown in Selenium

Once we declared Select then we have to pick a method to select dropdown values. So here Select methods enlisted with their detailed description, you can use any of them as per the business requirement or your convenience.

 

selectByVisibleText(String)

This method lets you select dropdown element with the text visible on the test website. For example: if there is a dropdown list of months and we have to select month March and it is visible then we will utilize this method as:

 

selectByValue(String)

This is one of the attributes of dropdown options. In above-inspected code, we see each option have some numeric value so we can select any dropdown options by passing their respective value as an argument in this method.

selectByIndex(int)

Each of the dropdown options has some indexing and it starts from index# 0 from the top. So if you have to select 6th element then you have to pick its index# as 5. Hence this method used below:

dropdown in selenium

getFirstSelectedOption()

Its return type is WebElement. Hence, if you have to get the first element in dropdown list then you can use this method. Further, you can implement the operations that we perform on any WebElement.

 

getOptions()

Its return type is WebElements, meaning it returns all the dropdown elements in a List, unlike, previous one where that was returning a single element. Further, we can iterate and do implementations for WebElements as per our requirements.

 

So these are the methods to select elements from the dropdown in WebDriver. Please find a demo code snippet below and further explanation is given in the video:

 

 

Related Articles

Add Comment