How to get selected option using Selenium WebDriver with Java
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 );
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);
In Selenium Python it is:
from selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support.ui import Selectdef 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