Play with DropDowns (Selenium)
1. To check whether a particular option available in drop down or not:
public class OptionPresentOrNotDropdown {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "pathofyourchrome.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://yoursite.com");
WebElement dropdownElement = driver.findElement(By.xpath("//select[@id='Store']"));
Select dropdown = new Select(dropdownElement);
List allElements = dropdown.getOptions();
System.out.println("Values present in Single Value Dropdown");
for (WebElement element : allElements) {
// This will iterate over each element and print the text
String dropdownValue = element.getText();
if (dropdownValue.equals("Selenium")) {
System.out.println("Selenium is present.)");
}
}
}
}
2. To check whether drop-down allows multi-selection or not:
WebElement dropdown = driver.findElement(By.xpath("//select[@id='Store']"));
String singleOrMulti = dropdown.getAttribute("multiple");
Or
WebElement dropdown = driver.findElement(By.xpath("//select[@id='Store']"));
boolean singleOrMultiple = dropdown.isMultiple();
Comments
Post a Comment