The dropdowns in our webpage do not use the common select tags or Li tags. this is making it difficult to extract and store the list items text in a WebElement list. Here is an image of the DOM and drop down list Vs and I also provide the code I am trying to write
`WebDriver driver = getDriver();
List<WebElement> teachers = driver.findElements(By.cssSelector(".rc-virtual-list-holder-inner>div>div"));
for (WebElement myElement : teachers) {
System.out.println(myElement.getText());`
Given the HTML...
to print the item texts ideally you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use the following locator strategies:
Using cssSelector:
List<WebElement> elements = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.rc-virtual-list-holder-inner div[title] > div")));
for(WebElement element : elements)
{
System.out.println(element.getText());
}
Using xpath:
List<WebElement> elements = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@class="rc-virtual-list-holder-inner"]//div[@title]/div")));
for(WebElement element : elements)
{
System.out.println(element.getText());
}
Using Java8's stream()
and map()
you can use the following locator strategies:
Using cssSelector:
System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.rc-virtual-list-holder-inner div[title] > div"))).stream().map(element->element.getText()).collect(Collectors.toList()));
Using xpath:
System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@class="rc-virtual-list-holder-inner"]//div[@title]/div"))).stream().map(element->element.getText()).collect(Collectors.toList()));