39

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

I am able to select any value from the 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 were in vain. How do I get the selected option?

daaawx
  • 3,273
  • 2
  • 17
  • 16
WomenInTech
  • 1,141
  • 2
  • 18
  • 25

6 Answers6

72

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 );
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Justin Ko
  • 46,526
  • 5
  • 91
  • 101
  • 11
    I would add for future readers that we need to import org.openqa.selenium.support.ui.Select to work on Select elements. – Michal Sep 05 '13 at 08:34
  • getFirstSelectedOption throws a org.openqa.selenium.NotFoundException when no option is selected. I would recommend catching that exception as a defensive measure. – Jim Kennedy Mar 28 '21 at 05:48
20

Completing the answer:

String selectedOption = new Select(driver.findElement(By.xpath("Type the xpath of the drop-down element"))).getFirstSelectedOption().getText();

Assert.assertEquals("Please select any option...", selectedOption);
Bhuvan
  • 2,209
  • 11
  • 33
  • 46
7

In Selenium Python it is:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select

def get_selected_value_from_drop_down(self):
    try:
        select = Select(WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.ID, 'data_configuration_edit_data_object_tab_details_lb_use_for_match'))))
        return select.first_selected_option.get_attribute("value")
    except NoSuchElementException, e:
        print "Element not found "
        print e
Riaz Ladhani
  • 3,946
  • 15
  • 70
  • 127
3

On the following option:

WebElement option = select.getFirstSelectedOption();
option.getText();

If from the method getText() you get a blank, you can get the string from the value of the option using the method getAttribute:

WebElement option = select.getFirstSelectedOption();
option.getAttribute("value");
Mithical
  • 603
  • 1
  • 17
  • 28
Miguel
  • 31
  • 1
0

short answer

Select select = new Select(driver.findElement(By.xpath("//select")));
System.out.println("selected items from the dropdown"+ select.getFirstSelectedOption().getText());
-1
var option = driver.FindElement(By.Id("employmentType"));
        var selectElement = new SelectElement(option);
        Task.Delay(3000).Wait();
        selectElement.SelectByIndex(2);
        Console.Read();
Madhu Ragi
  • 41
  • 3
  • 3
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – DimaSan Dec 08 '16 at 09:58