Handling Non-Select or Any Types Of dropdown In Selenium Webdriver

Selenium WebDriver provides a special class called Select which can be used only for drop down created using Select tag. You can not use this class methods for a Non-Select drop downs. This is a major limitation of Select class.

Drop downs in a website could be created in multiple ways. You will find maximum drop downs are created using ul and li tags. There are many drag and drop front end development tools which generates drop down codes dynamically. Sometimes you need to mouse hover on an element to see drop down options. In short, there should be a common way to handle any or most of drop down.

In this post, I will explain some common ways to handle any type of drop downs.

Click on drop down and iterate through options

This is a very common logic I see around. Steps are simple as below:-

  1. Click on drop down to make options visible.
  2. Get all options of drop down.
  3. Iterate through it and match with option to be selected.
  4. If found then click on option for selection.

Code snippet

You can write same logic using Sream API as below :-

Problem in above logic is that it is not optimal as it need to iterate over options. If your desired option to be selected in the list is at bottom position then whole options will be iterated and that will be a performance impact.

Optimal logic to select value in drop down without iteration

  1. Click on drop down to make options visible.
  2. Create a custom locator using option to be selected.
  3. Locate it and select.

Code Snippet

You can download/clone above sample project from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related post here, all API manual and automation related posts here and find frequently asked Java Programs here.

Many other topics you can navigate through menu.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Writing technical posts and creating YouTube videos are my hobbies.

1 thought on “Handling Non-Select or Any Types Of dropdown In Selenium Webdriver

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.