Basic Selenium

How to Handle Bootstrap Dropdown in Selenium WebDriver

Have u ever heard about handle Bootstrap dropdown in Selenium? If no, then today you will learn 2 new things today.

First one – What is bootstrap dropdown

The second one- How to Select values from the bootstrap dropdown.

 

We already have discussed how to work with traditional dropdowns and we have also explored multiple ways to handle the same but today we will see how to work with BootStrap dropdown.

The bootstrap dropdown is an enhanced part of the dropdown where you will deal with UL, LI, DIV, SPAN etc tag of HTML.

 

An example of Bootstrap dropdown is below.

handle BootStrap dropdown in Selenium

 

To handle this kind of drop-down we have to use findElements method and then we can run a for loop to get specific elements.

           Handle BootStrap Login/Popup window in Selenium

YouTube video for Handle Bootstrap Dropdown in Selenium

 

Complete program to handle bootstrap dropdown in Selenium Webdriver

 

import java.util.List;

import org.openqa.selenium.By;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;



public class BootStrap {


   public static void main(String[] args) throws InterruptedException {


       // Start firefox browser

       FirefoxDriver driver = new FirefoxDriver();


       // start the application

      driver.get("http://seleniumpractise.blogspot.in/2016/08/bootstrap-dropdown-example-for-selenium.html");


       // First we have to click on menu item then only dropdown items will display

      driver.findElement(By.xpath(".//*[@id='menu1']")).click();



       // adding 2 seconds wait to avoid Sync issue

       Thread.sleep(2000);



       // Dropdown items are coming in <a> tag so below xpath will get all

       // elements and findElements will return list of WebElements

       List<WebElement> list = driver.findElementsByXPath("//ul[@class='dropdown-menu']//li/a");



       // We are using enhanced for loop to get the elements

       for (WebElement ele : list)

       {

          // for every elements it will print the name using innerHTML

          System.out.println("Values " + ele.getAttribute("innerHTML"));



          // Here we will verify if link (item) is equal to Java Script

          if (ele.getAttribute("innerHTML").contains("JavaScript")) {

             // if yes then click on link (iteam)

             ele.click();



             // break the loop or come out of loop

             break;

          }

       }

       // here you can write rest piece of code

   }

}

 

You can also select the values directly using xpath  and CSS but that approach is not recommended because direct XPath might change.

In the above approach, we can pass a parameter directly so based on test data it will select the values from the list.

Hope you liked the above article if yes then comment below and share it with your friends.

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

34 thoughts on “How to Handle Bootstrap Dropdown in Selenium WebDriver

  1. rahul Munjal says:

    HI, I am still not able to handle the accordion in selenium.
    So could you please help me.

    1. Hi Rahul,

      Can you please elaborate on the issue which you are facing?

  2. Parul Chaudhary says:

    Hi,

    Thanks for this article! I was stuck in Bootstrap drop-down now I am able to select my value.

    1. Hi Parul,

      Glad to know that my finding helped you. Please feel free to ask your doubts at my blog…:)

  3. Praveen Kumar Gubendran says:

    Hi Mukesh,

    Thanks for valuable post, I have select the dropdown values using index number instead of values, because the dropdown values may get change based on data availbility, could you please help me on this.

    Thanks
    Praveen.

    1. Hi Praveen,

      In my above post, i’ve used xpath which helps me to get actual text but in your case you can use index of array to select option irrespective of visible text

      1. Ismail says:

        Hi Can you help me with google drop down. In google there is a menu where you can select google drive, google calender, gmai and etc.. How do I select google calender from the menu?

        1. Hi Ismail,

          These apps on google page are lying on a frame and you need to switch to the corresponding frame then go ahead. Please refer this link for more info https://vistasadprojects.com/mukeshotwani-blogs-v2/handle-frames-in-selenium/

          1. Ismail says:

            Hi Mukesh, it says no such frame. I don’t think it is frame…
            // Below is where I am stuck at
            driver.SwitchTo().Frame(elment);
            driver.FindElement(By.XPath(“//span[text()=’$0′]”)).Click();
            Thread.Sleep(3000);

          2. Hi Ismail,

            Switch frame using frame index instead of element and soon after frame switch, provide static wait of 2 sec and then proceed.

  4. Nice article about handling the Bootstrap dropdown using Selenium Webdriver.

    1. Hi Savithaa,

      Very very thanks for you comments….:)

  5. sudha says:

    Hi Mukesh,
    I just changed the xpath in ur above code for my project, am getting staleelementreference exception.
    My xpath is //div[@class=’selectize-dropdown-content’]//div”
    I think i’ve no problem with my xpath. could you please help me to sort it out
    Thanks , sudha

  6. Vineet says:

    Hi Mukesh,

    I am struggling with a bootstrap search box which generates a list of suggestions (typeheads) after I type in the search box. This element is basically a Type-in search box. Type in the box and you get a list of suggestions and then you have to select one of the suggested options. I am not able to handle that suggestion list. How do I select any value from that list?

    1. Hi Vineet,

      Using which web browser you have tried this?

      1. Vineet says:

        I was doing it on Chrome

        1. Hi Vinnet,
          Try same with FF and check whether you are able to see those suggestions inside DOM

  7. Aniket says:

    Hello Mukesh,
    I tried automating a website for sign in (iFrame) including email ,password and login button. I can enter fields for email and password (using script) but not able to click on Log in Button and forget password link.
    Please note that Login button and forget password link are included in iFrame.
    So what would be the reason for not able to perform mentioned operation ?

    Thanks in advance.

    1. Hi Aniket,

      Could you please mention what exception you are getting?

  8. Raghu says:

    Hi Mukesh,
    What is InnerHTML?

  9. Debanjan Bhattacharjee says:

    Hi Mukesh, Thanks for this useful video on Bootstrap Drop Down List.

    1. Hi Debanjan,

      Happy to read from you.

  10. vishwanath says:

    Hi Mukesh, is it possible to save chrome browser console output to file?

    1. Hi Vishwanath,
      Not specifically chrome browser console but whole console.

  11. This bellow Code, I am trying to click on a specific value from a bootstrap drop-down list. I can not do, please help me sir

    package practice;

    import java.util.List;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;

    public class Bootstrap {

    public static void main(String[] args) {

    WebDriver driver =new FirefoxDriver();
    driver.manage().window().maximize();

    driver.get(“http://seleniumpractise.blogspot.in/2016/08/bootstrap-dropdown-example-for-selenium.html”);

    driver.findElement(By.xpath(“.//*[@id=’menu1′]”)).click();

    List dd_menu= driver.findElements(By.xpath(“//ul[@class=’dropdown-menu’]//li/a”));

    for(int i=0;i<dd_menu.size();i++)
    {
    WebElement element =dd_menu.get(i);

    String innerhtml=element.getAttribute("innerHTML");

    if(innerhtml.contentEquals("JavaScript"))
    {
    element.click();
    break;
    }

    System.out.println("valur from drop is"+innerhtml);

    }

    }

    }

    1. Hi Santosh,

      This code is working fine for me. It is clicking correctly of JavaScript option from drop down menu

      1. priya says:

        its not clicking in my code…i wrote exactly the same code:
        List dd_menu= driver.findElements(By.xpath(“//[@class=’dropdown-menu’]/li/a”));
        for(int i=0;i<dd_menu.size();i++)
        {
        WebElement element= dd_menu.get(i);
        String innerhtml=element.getAttribute("innerHTML");
        if(innerhtml.contentEquals("LATUR"))
        {
        element.click();
        break;
        }
        System.out.println("values from dropdown is ——–" +innerhtml);

        }
        but it giving the following error
        Error communicating with the remote browser. It may have died.

        1. Hi Priya,

          It looks like browser related issue.

          1. priya says:

            is their any way to solve it??

          2. Hi Priya,

            Please tell me Selenium and Web Browser details…

  12. Shashi says:

    Hi Mukesh,
    Good article and I was stuck with finding element in Boostrap list, Later I verified your blog got solution and able to progress my Automation scripting.
    Thanks a lot
    Shashi

    1. Thanks Shasshi 🙂 Keep in touch

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.