How to check the drop down list item is selected

Answered
public static WebElement drpdwn_selectMonth() throws Exception{

try{

WebElement monthSelector = driver.findElement(By.id("monthID"));

monthSelector.click();

driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

monthSelector = driver.findElement(By.xpath("//*[@id='monthID']/option[2]"));

monthSelector.click();

driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}catch (Exception e){

throw(e);

}

return element;

}

1. how to do a Boolean check there is a value under drop down list is selected?

2. how to print the value selected in drop down list

Add Comment
  • 1 Answer(s)
    Best answer

    You have to use the Select class to work on Drop Down elements.
    However there is no specific way to to get a Boolean value for element selected in drop-down or not.

    WebElement select = driver.findElement(By.tagName("select"));
    
    select.deselectAll();
    
    select.selectByVisibleText("Value1");
    
    WebElement option = select.getFirstSelectedOption();
    
    //OR you can use "getAllSelectedOptions()" to return the list of elements selected.
    
    System.out.println(option);
    

    Supporter Answered on January 27, 2015.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.