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 in Selenium by Vaishnavi
• 1,170 points
174 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 by Surya
• 970 points

Related Questions In Selenium

0 votes
1 answer

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

Its pretty simple. Check out the below ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,110 points
8,866 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,110 points
2,407 views
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,110 points
1,136 views
0 votes
1 answer
0 votes
1 answer

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

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

answered Apr 18, 2018 in Selenium by king_kenny
• 3,650 points
373 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,150 points
1,240 views
0 votes
1 answer

How to save as PDF on Chrome using Selenium

Hey, Vaishnavi! You could try to disable ...READ MORE

answered Mar 8 in Selenium by Surya
• 970 points
5,578 views