How to get selected option using Selenium WebDriver with Java

0
0

I am newbie in Selenium and i am stuck here :

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

I am able to select any value from drop down but I am 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 going in vain
Any help would be appreciated . Thanks in advance 🙂

  • You must to post comments
0
0

You should be able to get the text using getText() (for the option element you got using getFirstSelectedOption()):

Select select = new Select(driver.findElement(By.xpath("//select")));
WebElement option = select.getFirstSelectedOption();
String defaultItem = option.getText();
System.out.println(defaultItem );
  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website