Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

How can I select an item from a drop-down list like gender (eg male, female) using Selenium WebDriver with Java?

I have tried this

WebElement select = driver.findElement(By.id("gender"));

List<WebElement> options = select.findElements(By.tagName("Male"));

for (WebElement option : options) {

    if("Germany".equals(option.getText()))

        option.click();   

}

My above code didn't work.

1 Answer

0 votes
by (62.9k points)

Use the below code:

new Select(driver.findElement(By.id("blood-group"))).selectByVisibleText("AB");

You also need to

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

To learn in-depth about Selenium, sign up for an industry based Selenium certification.

Browse Categories

...