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,090 points
366 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,710 points

Related Questions In Selenium

0 votes
3 answers

How to scroll down a webpage in selenium using Java?

JavascriptExecutor js = ((JavascriptExecutor) driver); js.executeScript("window.scrollTo(0, document.body.scrollHeight)"); or js.executeScript("arguments[0].scrollIntoView(true);", ...READ MORE

answered Sep 3 in Selenium by Sri
• 2,970 points
25,453 views
0 votes
1 answer
0 votes
1 answer

How to print text from a list of all web elements with same class name in Selenium?

@Rajni, following test case is somewhat similar ...READ MORE

answered Jun 20, 2019 in Selenium by Avantika
20,145 views
0 votes
1 answer

How to switch back from a frame to default in Selenium Webdriver?

Hey Rupesh, to switch back from a ...READ MORE

answered Jun 25, 2019 in Selenium by Anvi
• 14,110 points
5,450 views
0 votes
2 answers

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,520 points
3,746 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
4,730 views
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,890 points
2,724 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2 in Selenium by Sri
• 2,970 points
3,064 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,710 points
641 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,710 points
164 views