Handle Bootstrap Dropdown In Selenium Webdriver
In this tutorial will discuss on how to handle bootstrap dropdown in selenium webdriver.
Everyone is familiar with handling drop downs but when you work in real time you may or may not get the select class drop down. For example, there is boot strap drop down in this case we don’t have select class so obviously we don’t have methods like ‘selectByText()’, ‘selectByIndex()’ and ‘selectByValue()’.
To handle above problem, solution is first we need to grab all the elements present in drop down and loop through each element until you get your desired element and then you can click the element.
In selenium Webdriver it is one of the main functionality while dealing with web elements.
This is the best approach to handle bootstrap dropdown in selenium web driver also you can say that handle dropdown without select in selenium java.
In Below image you can able to see list of items if you want to select any specific element example if you want to select ‘Java’. You can iterate all the elements, when you found the element you can stop using break statement from java.
Find the sample code below for handling dropdown without select in selenium, I have handled single element , suppose if you want to select multiple items, yes you can do but you need to give break statement once you reach final element.
From below program you can able to find
1)How to get the count all elements from the dropdown.
2)How to get the name of all elements from the dropdown.
3)How to iterate all the elements in the dropdown.
4)How to stop at specific element.
All above points will be useful in interview. All the best ?
public class HandlingDropDown
public void BootStrapDropDown()
//Initialize reference to driver
//Setting the properties for chromedriver
//Initialize the chromedriver for webdriver reference
//Setting implicit wait to load all web elements in application
//Maximize the window
//Clicking the dropdown
//Collecting all the elements present in the dropdown
List<WebElement> allDropDownValues=driver.findElements(By.xpath("//ul[@class='multiselect-container dropdown-menu']/li/a/label"));
//Finding the count of all elements
int dropDownCount= allDropDownValues.size();
//Printing count of all items
System.out.println("Total items present in the dropdown : "+dropDownCount);
//For loop to go each and every element in the dropdown
//if condition to check specific element "Java"
//Clicking the desired element
//Stop continuing the loop as we already found the desired element
Note: I have used chrome driver . if you want to download driver you can get from selenium official website or simply you can click here.
Please provide your comments or you need more info on this. Thank you ?.
Very useful information…
Yes..you are absolutely right…I was fallen this situation previously..thinking why select class dropdown could not find selectByText(), selectByIndex() and selectByValue() methods…I spent more than 3days to find out how to achieve but your stuff is very simple,consice and clean…great one..
Thank for your kind words Joseph.
It was awesome..good finding man..
Can you do a video series on selenium?
Thanks, Santhosh. Yeah, I will do the video so soon.
I love this article.thanks for the information
Thanks for your appreciation Supriya.
Thanks for your appreciation John.
Thanks for the information
Nice information. Really in depth explanation and good example.
What’s up, yeah this article is really good and I have learned
lot of things from it concerning blogging. thanks.