Saturday, September 14, 2019

Post#56.Verify Particular Option is present in dropdown or not? (dropdown without select tag)


Verify Particular Option is present in dropdown or not? (dropdown without select tag)

Lets consider below example.
Check 'Truck' is present under dropdown 

<ul class="dropdown-menu">
    <li>Car</li>
    <li>Bus</li>
    <li>Truck</li>
    <li>Bike</li>
  </ul>


// find the dropdown using xpath
                        WebElement dropdownElement = driver.findElement(By.xpath("//ul[@class='dropdown-menu']"));
                        //capture all options
                        List<WebElement> dropDownOptions = dropdownElement.findElements(By.tagName("li"));
                        for(WebElement element : dropDownOptions) {
                                    String dropdownOptionValue = element.getText();
                                    if (dropdownOptionValue.equals("Truck")) {
                                                System.out.println("Truck Option is present :)");
                                    }
}

No comments:

Post a Comment