
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 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class HandlingDropDown { @Test public void BootStrapDropDown() { //Initialize reference to driver WebDriver driver; //Setting the properties for chromedriver System.setProperty("webdriver.chrome.driver","./drivers/chromedriver.exe"); //Initialize the chromedriver for webdriver reference driver=new ChromeDriver(); //Launching application driver.get("https://www.jquery-az.com/boots/demo.php?ex=63.0_2"); //Setting implicit wait to load all web elements in application driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); //Maximize the window driver.manage().window().maximize(); //Clicking the dropdown driver.findElement(By.xpath("//button[@data-toggle='dropdown']")).click(); //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 for(int i=0;i<dropDownCount;i++) { //if condition to check specific element "Java" if(allDropDownValues.get(i).getText().contains("Java")) { //Clicking the desired element allDropDownValues.get(i).click(); //Stop continuing the loop as we already found the desired element break; } } } } |
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…
Thanks Rawoof
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 bro?
Great information
Thanks for your appreciation John.
Thanks for the information
Great infi
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.