Saturday, September 14, 2019

Post#57.Different ways to check whether Option is selected or not in Dropdown with Selenium WebDriver


Different ways to check whether Option is selected or not in Dropdown with Selenium WebDriver

We can verify whether an option is selected or not in a dropdown using selenium WebDriver, and there are different ways to do it.

1. isSelect() method
2. Verify with getFirstSelectedOption()
3. Use getAttribute() method
4. Use Java script Method


isSelected() method to verify option is selected or not in dropdown

isSelected() method is used to see whether a dropdown is selected or not.

getFirstSelectedOption() to check whether option is selected or not in selenium webdriver

getFirstSelectedOption() method provides us option element which is selected currently, we need to extract the text from the element and verify the text with expected result.

                        // find the dropdown using xpath
                        WebElement dropdownElement = driver.findElement(By.xpath("//select[@id='first']"));
                        // create object for Select class
                        Select dropdown = new Select(dropdownElement);
                        // select 1st selected option from the dropdown options
                        WebElement selectedOption = dropdown.getFirstSelectedOption();
                        // get the text from the option
                        String selectedOptionText = selectedOption.getText();
                        // print the text
                        System.out.println("Selected option from the dropdown is : "+selectedOptionText);


No comments:

Post a Comment