Select Class in Selenium WebDriver | Dropdown Handling Tutorial

 

  • In Selenium WebDriver, the Select class is used to interact with dropdown (select) elements on a web page.
  • Dropdowns are HTML <select> elements that allow users to choose from a list of options.
  • The Select class provides methods to select options, deselect options (in case of multi-select dropdowns), and perform various operations on dropdowns.
  • It's typically used for working with <select> elements created using the <select> and <option> HTML tags.


Here are some of the most commonly used methods of the Select class along with explanations:
1. Creating a Select object:
  • To work with a dropdown, you first need to create a Select object by locating the <select> element.
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;

WebDriver driver = new ChromeDriver();
driver.get("https://example.com");

WebElement dropdownElement = driver.findElement(By.id("dropdown"));

// Create a Select object
Select dropdown = new Select(dropdownElement);
  
2. Selecting options:
  • To select an option by its visible text:
dropdown.selectByVisibleText("Option Text");
  
  • To select an option by its index (0-based):
dropdown.selectByIndex(1); // Selects the second option
  
  • To select an option by its attribute value (e.g., value attribute):
dropdown.selectByValue("option-value");
  
3. Deselecting options (for multi-select dropdowns):
  • To deselect an option by its visible text:
dropdown.deselectByVisibleText("Option Text");
  
  • To deselect an option by its index:
dropdown.deselectByIndex(1);
  
  • To deselect an option by its attribute value:
dropdown.deselectByValue("option-value");
  
4. Deselecting all options (for multi-select dropdowns):
dropdown.deselectAll();
  
5. Getting selected options:
  • To get the selected option(s) as WebElement:
WebElement selectedOption = dropdown.getFirstSelectedOption();
  
  • To get all selected options as a list of WebElement objects (for multi-select dropdowns):
List<WebElement> selectedOptions = dropdown.getAllSelectedOptions();
  
6. Checking if it's a multi-select dropdown:
boolean isMultiple = dropdown.isMultiple();
  
7. Getting all options in the dropdown:
  • To get all options as a list of WebElement objects:
List<WebElement> options = dropdown.getOptions();
  
8. Getting the count of options:
int optionCount = dropdown.getOptions().size();
  
9. Verifying if an option is present:
  • To check if an option with a specific visible text is present in the dropdown:
boolean isOptionPresent = dropdown.getOptions().stream().anyMatch
(option -> option.getText().equals("Option Text"));
  
10. Verifying if an option is selected:
  • To check if a specific option is selected:
boolean isSelected = dropdown.getFirstSelectedOption().getText().
equals("Option Text");
  
  • These methods allow you to interact with and manipulate dropdowns using Selenium WebDriver.
  • Note that not all methods are applicable to all dropdowns, as their behavior can vary based on whether the dropdown allows multiple selections and how it's implemented on the web page.
  • Always refer to the HTML structure and behavior of the specific dropdown you're working with when using the Select class methods.

Post a Comment

0 Comments