How to get the select options using Selenium WebDriver in Java

0 votes

I am very new to Selenium and I'm stuck here:

I want to get the selected label or value of a drop down using Selenium WebDriver and then print it on the console.

I'm just able to select any value from the drop-down but not able to retrieve the selected value and print it.

Select select = new 
Select(driver.findElement(By.id("MyDropDown"))).selectByVisibleText(data[11].substring(1 , data[11].length()-1));
WebElement option = select.getFirstSelectedOption();

But all my efforts are going in vain. Any help would be appreciated. 

Jun 13, 2019 in Selenium by Vaishnavi
• 1,180 points
3,414 views

1 answer to this question.

0 votes

Hey there!

You should be able to get the text using getText() if you have an option element, you can get that using getFirstSelectedOption());

Select select = new Select(driver.findElement(By.xpath("//select")));
WebElement option = select.getFirstSelectedOption();
String defaultItem = option.getText();
System.out.println(defaultItem );

Hope this helps. 

answered Jun 14, 2019 by Surya
• 970 points

Related Questions In Selenium

0 votes
2 answers

How to scroll the Page up or down in Selenium WebDriver? (using java)

JavascriptExecutor jsx = (JavascriptExecutor)driver; jsx.executeScript("window.scrollBy(0,555)", ""); or Action classes ...READ MORE

answered Sep 6, 2020 in Selenium by Sri
• 3,190 points
16,426 views
+1 vote
1 answer

How to get the HTML source of a webpage using Selenium in Java?

There is a method called getPageSource() in ...READ MORE

answered Jan 11, 2019 in Selenium by Sneha
22,663 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
7,394 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
7,031 views
0 votes
2 answers

What is the role of TestNG & JUnit frameworks in Selenium?

TestNG and JUnit are test frameworks . it ...READ MORE

answered Sep 4, 2020 in Selenium by Sri
• 3,190 points
1,522 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
4,718 views
0 votes
2 answers
0 votes
2 answers

How to save as PDF on Chrome using Selenium

Vaishanvi, Select  ID  selected dropdown or it ...READ MORE

answered Feb 22, 2020 in Selenium by Abdul
27,814 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP