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,010 points
27 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,590 points

Related Questions In Selenium

+1 vote
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
389 views
0 votes
1 answer
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,030 points
1,234 views
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,030 points
73 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
446 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
55 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,590 points
145 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,590 points
23 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.