简体   繁体   中英

Selenium Web Driver - How to check whether all the products in a page loads

I am trying to solve an issue where I need to check whether all the products in a web page loads completely. The products load only when the user scroll the page downwards. Each time on a scroll 8 products are loaded.

How to check in that page that the last product loads and user is now not able to scroll downwards?

I faced a similar issue as you. In my case I got a message saying more items were being loaded. Once I had loaded all items I no longer got this message. So the way I solved it was by creating a loop which scrolls to the bottom of the page, checks if more stuff is being loaded and breaks out of the loop once all is loaded.

Now if you don't have an element that indicated more items are loading you could use findElements() to get all the products and check the size of this list. Once all products are loaded the list will remain the same length.

JavascriptExecutor executor = (JavascriptExecutor) driver;
List<WebElement> products = driver.findElements(By.something, "locator");

int amount = 0;
while products.size() > amount {
    amount = products.size()
    // Scroll to the bottom of the page
    executor.executeScript("window.scrollTo(0, document.body.scrollHeight);");
    products = driver.findElements(By.something, "locator");
}

Use the known solution for scrolling down :

WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");

Then check that 8 products after last scrolling iteration doesn't differ from current iteration. pseudo-code:

products = getProducts();
jse.executeScript("window.scrollBy(0,250)", "");
// reasonable wait here
if(getProducts() == products){
    //that it. nothing to scroll 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM