Saturday, September 14, 2019

Post#55.Verify Particular Option is present in dropdown or not ? (For dropdown with select tag)


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

We can verify whether a particular option is present or not in selenium webdriver.

Scenario : 
1. Navigate to dropdown Practice Page : 
https://the-internet.herokuapp.com/dropdown

2. Check 'Option 1' is present under dropdown 


Steps to find Option present in dropdown or not in selenium webdriver
1. Find the dropdown using findElement Method
2. Create object for Select class passing above found element
3. use getOptions() method present in Select class object and store values(it returns webelement in list) in List
4. iterate the list to get the text from the Options one by one
5. compare the result text with expected result.

//open webpage
driver.get("application-url");
//find the dropdown using xpath
WebElement dropdownElement = driver.findElement(By.xpath("//select[@id='animals']"));
//create object for Select class
Select dropdown = new Select(dropdownElement);
//get all the options and store it in list
List allElements = dropdown.getOptions();
System.out.println("Values present in Single Value Dropdown");
for (WebElement element : allElements) {
            // iterate over each element and print the text
            String dropdownOptionValue = element.getText();
            if (dropdownOptionValue.equals("Option1")) {
                        System.out.println("Option 1 is present :)");
            }
}

No comments:

Post a Comment