22. Automating HTML Select & Custom Dropdown Widgets

Selenium Select Class

import org.openqa.selenium.support.ui.Select;

WebElement dropdownElement = driver.findElement(By.id("country-dropdown"));
Select countrySelect = new Select(dropdownElement);

// Selection options:
countrySelect.selectByVisibleText("United States");
countrySelect.selectByValue("US");
countrySelect.selectByIndex(1);

// Verifying selected option:
String chosen = countrySelect.getFirstSelectedOption().getText();

Task

  • Implement selectByTextSafe:
  • 1. Accept WebElement element and String visibleText.
  • 2. Initialize Select select = new Select(element).
  • 3. Call select.selectByVisibleText(visibleText).
πŸ’‘Common doubts
Did you like the problem?
Lesson 22 of 44
Java
Loading code editor…
Next