• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Basic Selenium / How to Handle Bootstrap Dropdown in Selenium WebDriver

How to Handle Bootstrap Dropdown in Selenium WebDriver

October 25, 2020 by Mukesh Otwani 34 Comments

handle BootStrap dropdown in Selenium

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.

Filed Under: Basic Selenium Tagged With: handle BootStrap dropdown in Selenium

Reader Interactions

Comments

  1. rahul Munjal says

    May 5, 2022 at 11:44 PM

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

    Reply
    • Mukesh Otwani says

      May 6, 2022 at 9:42 AM

      Hi Rahul,

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

      Reply
  2. Parul Chaudhary says

    May 14, 2020 at 6:49 PM

    Hi,

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

    Reply
    • Mukesh Otwani says

      May 14, 2020 at 8:47 PM

      Hi Parul,

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

      Reply
  3. Praveen Kumar Gubendran says

    June 17, 2019 at 5:47 PM

    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.

    Reply
    • Mukesh Otwani says

      June 17, 2019 at 6:20 PM

      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

      Reply
      • Ismail says

        March 29, 2020 at 6:43 AM

        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?

        Reply
        • Mukesh Otwani says

          March 29, 2020 at 9:18 PM

          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 http://learn-automation.com/handle-frames-in-selenium/

          Reply
          • Ismail says

            April 1, 2020 at 8:14 AM

            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);

          • Mukesh Otwani says

            April 1, 2020 at 4:06 PM

            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. Savithaa Nirmal says

    April 1, 2017 at 11:05 PM

    Nice article about handling the Bootstrap dropdown using Selenium Webdriver.

    Reply
    • Mukesh Otwani says

      April 1, 2017 at 11:33 PM

      Hi Savithaa,

      Very very thanks for you comments….:)

      Reply
  5. sudha says

    March 31, 2017 at 9:19 PM

    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

    Reply
    • Mukesh Otwani says

      April 1, 2017 at 10:50 AM

      Hi Sudha,

      In this case, you must refer this link http://learn-automation.com/how-to-solve-stale-element-reference-exception-in-selenium-webdriver/

      Reply
  6. Vineet says

    March 25, 2017 at 12:05 AM

    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?

    Reply
    • Mukesh Otwani says

      March 25, 2017 at 11:08 PM

      Hi Vineet,

      Using which web browser you have tried this?

      Reply
      • Vineet says

        March 25, 2017 at 11:10 PM

        I was doing it on Chrome

        Reply
        • Mukesh Otwani says

          March 25, 2017 at 11:30 PM

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

          Reply
  7. Aniket says

    March 16, 2017 at 12:07 AM

    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.

    Reply
    • Mukesh Otwani says

      March 20, 2017 at 11:28 AM

      Hi Aniket,

      Could you please mention what exception you are getting?

      Reply
  8. Raghu says

    March 3, 2017 at 12:56 PM

    Hi Mukesh,
    What is InnerHTML?

    Reply
    • Mukesh Otwani says

      March 3, 2017 at 4:21 PM

      Hi Raghu,

      Please check this post https://www.w3schools.com/jsref/prop_html_innerhtml.asp

      Reply
  9. Debanjan Bhattacharjee says

    February 12, 2017 at 2:05 AM

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

    Reply
    • Mukesh Otwani says

      February 12, 2017 at 3:41 PM

      Hi Debanjan,

      Happy to read from you.

      Reply
  10. vishwanath says

    February 8, 2017 at 10:34 AM

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

    Reply
    • Mukesh Otwani says

      February 11, 2017 at 2:17 PM

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

      Reply
  11. santosh patange says

    February 6, 2017 at 4:59 PM

    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);

    }

    }

    }

    Reply
    • Mukesh Otwani says

      February 6, 2017 at 5:46 PM

      Hi Santosh,

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

      Reply
      • priya says

        March 23, 2017 at 4:13 PM

        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.

        Reply
        • Mukesh Otwani says

          March 23, 2017 at 4:55 PM

          Hi Priya,

          It looks like browser related issue.

          Reply
          • priya says

            March 23, 2017 at 5:12 PM

            is their any way to solve it??

          • Mukesh Otwani says

            March 24, 2017 at 10:48 AM

            Hi Priya,

            Please tell me Selenium and Web Browser details…

  12. Shashi says

    December 29, 2016 at 12:16 PM

    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

    Reply
    • Mukesh Otwani says

      January 11, 2017 at 3:31 PM

      Thanks Shasshi 🙂 Keep in touch

      Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?