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
1,502 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 13, 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 5 in Selenium by Sri
• 2,970 points
13,263 views
0 votes
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 10, 2019 in Selenium by Sneha
11,638 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,510 points
3,531 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,535 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 in Selenium by Sri
• 2,970 points
833 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,596 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 21 in Selenium by Abdul
16,584 views