How to Handle drop down in Selenium Web Driver

In this tutorial, we are going to focus on the Dropdown menu. How to automate the Drop Dow menu using selenium web driver and all the key points to Handle drop down in Selenium Web Driver

Handle drop down in Selenium Web Driver

Select Class in Selenium web driver. 

Select class or its methods are used to select or deselect options from a dropdown.

Select class takes an element of type WebElement as a constructor.

For Example:

// find all the elements which is avalible in Select tag
     WebElement elements = driver.findElement(By.id("select-demo"));
  
 //Create an object of Select class and pass the elements as parameter
  Select optionSelect = new Select(elements);

How to select option form drop down:

 selectByIndex – select by the index of option like 0,1,2 etc……….

//Select option by Value attribute.

 optionSelect.selectByValue("Monday");

selectByValue – Select by the value of options.

//Select option by Value attribute.

 optionSelect.selectByValue("Monday");

selectByVisibleText – Select by GUI text of options.

//Select option by Text or GUI text.
optionSelect.selectByVisibleText("Sunday");

The following are the useful methods of Select Class.

getOptions() – This method returns list of all the options(List<WebElement>).

getAllSelectedOptions() – This method will return list of all the selected options.

getFirstSelectedOption() – This method will return 1st selected option in case of multi-select.

isMultiple() – This method will return a true or false value (boolean value). It checks drop-down is allow Multi-select option or not.

deselectAll() – This method deselect all the selected options.

deselectByIndex(int index) – Deselect the option by its index.

deselectByValue(String valueAttribute) – Deselect the option its attribute value.

deselectByVisibleText(String text) – Deselect the option by GUI text or visible text.

Handle drop down in Selenium Web Driver with example

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

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

  System.setProperty("webdriver.chrome.driver", "D:\\Automation\\selenium\\lib_files\\chrome\\chromedriver.exe");

  // Open Browser
  WebDriver driver = new ChromeDriver();

  // Open the URL in current window of the browser
  driver.get("http://www.seleniumeasy.com/test/basic-select-dropdown-demo.html");

  /************************ drop down ****************************************/
  // find all the elements which is avalible in Select tag
  WebElement elements = driver.findElement(By.id("select-demo"));

  // Create an object of Select class and pass the elements as parameter
  Select optionSelect = new Select(elements);

  // Select option by index ..... index start from 0.
  optionSelect.selectByIndex(0);

  // Select option by Text or GUI text.
  optionSelect.selectByVisibleText("Sunday");

  // Select option by Value attribute.
  optionSelect.selectByValue("Monday");

  // To get all the option from the drop down list.

  WebElement select_elements = driver.findElement(By.xpath("//*[@id=\"multi-select\"]"));

  // Pass element to select class
  Select select_option = new Select(select_elements);
  
  // Get the value of all the options and store in a list of type WebElement.
  List<WebElement> list = select_option.getOptions();

  // Retrieve the value from the list uisng loop
  for (WebElement l : list) {

   System.out.println(l.getText());
  }

 }
}

 

 

 

Add a Comment

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

seven + sixteen =