Wednesday, 25 April 2018

How to Handle Bootstrap Dropdown in Selenium WebDriver

In our previous post we have already seen How to Handle Dropdowns in Selenium WebDriver . In this post we will see how to handle Bootsrap Dropdown.
Bootstrap dropdowns and interactive dropdowns which are dynamically positioned and formed using list of <ul> and <li> html tags.

Below is the simple example of Bootstrap Dropdown-

Video Tutorial - 

Let us see sample selenium code -

package test;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

public class BootstrapDropDown {
 ChromeDriver driver;
 public  void setup(){
  System.setProperty("", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
      driver=new ChromeDriver();

        // start the application
 public void testBootStrap() throws Exception
        // First we have to click on menu item then only dropdown items will display
        // adding 2 seconds wait to avoid Sync issue
        // 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 test']//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)
              // break the loop or come out of loop
        // here you can write rest piece of code
public void tearDown()



Post a Comment