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
1,157 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
5 answers

How to scroll down a webpage in selenium using Java?

 to scroll up or down with Selenium, a JavaScript executor is a ...READ MORE

answered Dec 16, 2020 in Selenium by Roshni
• 10,480 points
44,651 views
0 votes
1 answer
0 votes
3 answers

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

Try using List <WebElement> to access all similar elements ...READ MORE

answered Dec 16, 2020 in Selenium by Roshni
• 10,480 points
87,453 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,150 points
12,365 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,500 points
13,686 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,149 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,910 points
7,227 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 3, 2020 in Selenium by Sri
• 3,190 points
7,879 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
1,680 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 14, 2018 in Selenium by king_kenny
• 3,710 points
1,037 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP