Handling DropDown And Multiple Select in Webdriver using Selenium

SeleniumAutomation TestingTesting Tools

We can handle multi-select dropdown with Selenium webdriver using the Select class. A multi-select dropdown is the one which allows selection of multi options.

The Select methods to handle multi-select dropdown are listed below −

  • getOptions – returns the list of all options in the dropdown.

Select s = new Select(e);
List<WebElement> l = s.getOptions();
  • getFirstSelectedOption– returns the selected option in the dropdown. If there are multi options selected, only the first item shall be returned.

Select s = new Select(e);
l = s. getFirstSelectedOption();
  • isMultiple – returns a boolean value, yields a true value if the dropdown allows selection of multiple items.

Select s = new Select(e);
boolean l = s.isMultiple();
  • selectByIndex – The index of the option to be selected by the dropdown is passed as a parameter. The index starts from 0.

WebElement e = driver.findElement(By.id("slc"));
Select s = new Select(e);
s.selectByIndex(0);
  • selectByValue – The value attribute of the option to be selected by the dropdown is passed as a parameter. The options in the dropdown should have the value attribute so that this method can be used.

WebElement e = driver.findElement(By.id("slc"));
Select s = new Select(e);
s.selectByValue("option 1");
  • deselectByVisibleText – The visible text of the option to be deselected by the dropdown is passed as a parameter. It is only applicable to multi-select dropdowns.

WebElement e = driver.findElement(By.id("slc"));
Select s = new Select(e);
s.deselectByVisibleText("Selenium");
  • deselectByIndex – The index of the option to be deselected by the dropdown is passed as a parameter. The index starts from 0. It is only applicable to multi-select dropdowns.

WebElement e = driver.findElement(By.id("slc"));
Select s = new Select(e);
s.deselectByIndex(0);
  • deselectByValue – The value attribute of the option to be deselected by the dropdown is passed as a parameter. It is only applicable to multi-select dropdowns.

WebElement e = driver.findElement(By.id("slc"));
Select s = new Select(e);
s.deselectByValue("option 1");
  • selectByVisibleText – The visible text of the option to be selected by the dropdown is passed as a parameter.

WebElement e = driver.findElement(By.id("slc"));
Select s = new Select(e);
s.selectByVisibleText("Selenium");
  • deselectAll – unselects all selected options from the dropdown.

WebElement e = driver.findElement(By.id("slc"));
Select s = new Select(e);
s.deselectAll();

A multi-select dropdown has a select tag and its items are represented by the option tag.

Example

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.Select
public class MultiDrpDwn{
   public static void main(String[] args) {
      System.setProperty("webdriver.gecko.driver",
         "C:\\Users\\ghs6kor\\Desktop\\Java\\geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //maximize browser
      driver.manage().window().maximize();
      //URL launch
      driver.get("https://www.tutorialspoint.com/selenium/selenium_automation_practice.htm");
      //identify dropdown
      WebElement d= driverfindElement(By.xpath("//select[@name='selenium_commands']"));

      //object of Select class
      Select s=new Select(d);
      //get options of dropdown in list
      List t =s.getOptions();
      System.out.println("Options are: ");
      for (WebElement i: t){
         System.out.println(i.getText());
      }
      //return true if multi-select dropdown
      Boolean b=s.isMultiple();

      System.out.println("Boolen value for drodown: "+ b);
      //select item by index
      s.selectByIndex(2);
      Thread.sleep(1000);
      //select item by visible text
      s.selectByVisibleText("Wait Commands");
      Thread.sleep(1000);
      //get first selected option in dropdown
      WebElement f = s.getFirstSelectedOption();
      System.out.println("First selected option is: "+ f.getText());
      //deselect option by index
      s.deselectByIndex(2);
      Thread.sleep(1000);
      //deselect all selected items
      s.deselectAll();
      driver.close();
   }
}

Output

raja
Published on 07-Apr-2021 12:25:57
Advertisements