Saturday, September 14, 2019

Post#53.Get All Options Present in Dropdown


Get All Options Present in Dropdown

getOptions() - method return all the options present in dropdown irrespective of whether selected or not
//open webpage
driver.get("application-url");
//find the dropdown using xpath
WebElement dropdownElement = driver.findElement(By.xpath("//select[@id='first']"));
//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
            System.out.println(element.getText());
}
System.out.println("************************************");
WebElement dropdownElementMulti = driver.findElement(By.xpath("//select[@id='second']"));
Select dropdownMulti = new Select(dropdownElementMulti);
//get all the options and store it in list
List<WebElement> allElementsMulti = dropdownMulti.getOptions();
System.out.println("Values present in Multi value Dropdown");
for (WebElement elementMulti : allElementsMulti) {
            // iterate over each element and print the text
            System.out.println(elementMulti.getText());
}

No comments:

Post a Comment