I'm using Selenium to read X number of elements from a website. The website initially opens with 21 elements a "see more" button, upon clicking the button there's an extra batch of elements loaded and then another batch is loaded every time you scroll to the bottom of the page. What's the best way to keep scrolling down until e.g. 100 elements are loaded?
driver.findElement(By.cssSelector("[class='btn btn-primary btn-alternate search-more']")).click();
Thread.sleep(2000);
((JavascriptExecutor) driver)
.executeScript("window.scrollTo(0, document.body.scrollHeight)");
List<WebElement> elements = driver.findElements(By.className("project-tile"));
int elementsCount = elements.size();
CodePudding user response:
I can't give you tested answer, but I would suggest the following:
Count the amount of presented elements and scroll until this counter is less than the goal, as following:
driver.findElement(By.cssSelector("[class='btn btn-primary btn-alternate search-more']")).click();
Thread.sleep(2000);
int goal = 100;
int elementsCount = 0;
List<WebElement> elements = driver.findElements(By.className("project-tile"));
elementsCount = elementsCount elements.size();
while(elementsCount<goal){
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
elements = driver.findElements(By.className("project-tile"));
elementsCount = elementsCount elements.size();
}