Rajesh Natarajan
Rajesh Natarajan

Reputation: 3

How to verify element/text is present in dropdown list using selenium java

How to verify element/text is present in drop down list using selenium java

new Select(driver.findElement(By.id("airlineid"))).selectByVisibleText("Delta");

Upvotes: 0

Views: 43864

Answers (3)

Ranjitha Hegde
Ranjitha Hegde

Reputation: 1

This helped me :

Select select = new Select(select_drop_down);
List<WebElement> all_options = select.getOptions();
boolean found = false;

for (WebElement we : all_options) { 
    if (facility.equalsIgnoreCase(we.getAttribute("value"))) {
        select.selectByValue(facility);
        found = true;
        break;
    }
}

Upvotes: 0

Cosmin
Cosmin

Reputation: 2394

This can be done by using the Select class. To get the selected option that is visible:

WebElement select = driver.findElement(By.id("airlineid"));
WebElement option = select.getFirstSelectedOption();
String selectedValueInDropDown = option.getText();

To get all the options:

Boolean found = false;

WebElement element = driver.findElement(By.id("..."));
Select select = new Select(element);
List<WebElement> allOptions = select.getOptions();
for(int i=0; i<allOptions.size(); i++) {
    if(alloptions[i].Equals("your_option_text")) {
        found=true;
        break;
    }
}
if(found) {
    System.out.println("Value exists");
}

Upvotes: 2

Siva
Siva

Reputation: 1149

I dont know java, but i can give you code in c#, you can modify if as per java methods and coding standards

Solution1

IWebElement dropDownElement = driver.FindElement(By.Id("continents"));
        if (dropDownElement.Text.Contains("the value you are looking for here"))
        {
            // value is present in drop down
        }
        else
        {
            // value is not present
        }

Solution2

IList<IWebElement> optionsofDropDown = driver.FindElements(By.XPath("//*[@id='continents']/option"));

foreach (IWebElement optionVal in optionsofDropDown)
{
   if (optionVal.Text == "the value you are looking for here")
   {
       //value available in drop down
        break;
    }
    else
    {
       //value available in drop down
    }
  }

Upvotes: 0

Related Questions