It is not printing all the values from drop down. It is printing values up to the element that I'm making it to click on and printing blank for rest of the values.
driver.get("https://some-url.com");
driver.findElement(By.xpath("//*[@id='example']/div[4]/div/div[2]/div[4]/div[1]/div[6]/div/input[2]")).click();
List<WebElement> allOptions = driver.findElements(By.xpath("//div[@class='menu transition visible']/div"));
for (int i = 0; i < allOptions.size(); i++) {
System.out.println(allOptions.get(i).getText());
if (allOptions.get(i).getText().equalsIgnoreCase("Angola")) {
allOptions.get(i).click();
}
}
I expect my code to print all the values from drop down but it is actually printing values up to the element that I'm making it to click.
You are seeing the expected behavior. If your usecase is to print all the values and then click()
on the element with text as Angola, you can use the following Locator Strategies:
driver.get("https://some-url.com");
driver.findElement(By.xpath("//*[@id='example']/div[4]/div/div[2]/div[4]/div[1]/div[6]/div/input[2]")).click();
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@class='menu transition visible']/div"))).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList()););
List<WebElement> allOptions = driver.findElements(By.xpath("//div[@class='menu transition visible']/div"));
for (int i = 0; i < allOptions.size(); i++) {
if (allOptions.get(i).getText().equalsIgnoreCase("Angola")) {
allOptions.get(i).click();
break;
}
}