How to Handle Dropdown in Selenium webdriver

Hello, Guys, Welcome to Selenium tutorial, today in this post we will see how to handle Dropdown in Selenium webdriver.

Generally, while working with the script we have to select some values from the dropdown and do other activity and validate. The dropdown is just like simple WebElement like Checkbox, textbox etc.

 

Before moving to Dropdown in Selenium webdriver check out two useful plugins that will help you while writing scripts.

XPath plugin for Firefox 

Xpath plugin for Chrome

 

Please refer the video for the same.

 

 

 

 

Dropdown in Selenium webdriver

Select values from Dropdown in Selenium webdriver

For handling dropdowns Selenium already provides Select class that has some predefined method which help is a lot while working with Dropdown.

Select class can be find in below package.

 

Select values Dropdown in Selenium webdriver

Let’s Discuss some of the method and will see detailed program as well.

  • Select value using Index.

WebElement month_dropdown=driver.findElement(By.id(“month”));

Explanation- Here selectbyIndex(int) is method which accept integer as argument so depends on index it will select values. If you give index as 4, it will select 5th value.

Note- Index will start from 0.

 

  • Select value using value attribute.

Explanation- Here selectByValue(String) is a method which accept values it means whatever value you have in your dropdown. Please refer below screenshot to see how to get values from dropdown.

In our example we have taken value as 5 it means it will select May from dropdown.

 

  • Select value from Visible text

Explanation- We can also select value from text as well. This is very straight forward that whatever text we are passing it simply select that value.

Note- This is case sensitive it means if I pass Aug and dropdown has aug then Selenium will not be able to select value and it will fail your program so make sure Text which you are passing is correct.

In above example I am passing Aug in argument so it will select aug from dropdown.

 

  • Get Selected option from Dropdown.

Explanation- Here getFirstSelectedOption() will return the first selected option and will return you the WebElement then you can use getText() method to extract text and validate the same based on your requirement

 

  • Get All option from dropdown

Explanation- getOptions() is a method of Select class which will return List of WebElement then we can iterate using for loop or iterator and using getText() method we can extract values.

I have another very useful video which will show you how you can handle dropdown which does not have any Select class as well.

This is called BootStrap Dropdown and you can handle them using findElements method. I have created video for the same.

Hope you have enjoyed the article and videos. If yes then do share with your friends and let me know if any help required from my side.

For More updates Learn Automation page

For any query join Selenium group- Selenium Group

40 Comments

  1. Arjun
  2. RC
  3. Abhinav
  4. Somanath
  5. shabana banu
  6. Raju Raipure
  7. pavani
  8. Amit Chaudhary
    • Atul gupta
  9. Amit Chaudhary