How to choose from a drop-down in Selenium 2?

0 votes

I am converting my code from selenium1 to selenium2. And i'm not able to find a simple way to select a label in a drop down menu or get the selected value of a drop down. Do you know how to do that in Selenium 2?

Here are two statements that work in Selenium 1 but not in 2:

browser.select("//path_to_drop_down", "Value1");
browser.getSelectedValue("//path_to_drop_down");
Apr 24, 2018 in Selenium by kappa3010
• 2,020 points
129 views

1 answer to this question.

0 votes

You can probably use the 'Select' class that comes with WebDriver. You can select one based on visible text.

Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));
select.deselectAll();
select.selectByVisibleText("Value1");

You can use xpath or maybe cssSelector or name or id locators. To get the first selected value:

WebElement option = select.getFirstSelectedOption()

Or maybe use WebDriver default functions like below.

driver.findElement(By.id("id_dropdown_menu")).click();
driver.findElement(By.xpath("xpath_of-element")).click();
answered Apr 24, 2018 by king_kenny
• 3,650 points

Related Questions In Selenium

0 votes
2 answers

How to scroll down a webpage in selenium using Java?

This works... JavascriptExecutor jse = (JavascriptExecutor)driver; j ...READ MORE

answered Apr 3 in Selenium by anonymous
4,605 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
789 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,031 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
734 views
0 votes
1 answer

How to choose the correct iframe in Selenium WebDriver?

When I checked the website you've mentioned, ...READ MORE

answered Apr 29, 2018 in Selenium by king_kenny
• 3,650 points
259 views
0 votes
1 answer

How to select an Object by its class in Selenium?

When it comes to Selenium, XPath will ...READ MORE

answered Apr 13, 2018 in Selenium by king_kenny
• 3,650 points
59 views