[英]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. 每次滚动时,将加载8个产品。
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. 现在,如果您没有一个元素指示正在加载更多项目,则可以使用findElements()
获取所有产品并检查此列表的大小。 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. 然后检查last
滚动迭代后的8个产品与current
迭代是否相同。 pseudo-code: 伪代码:
products = getProducts();
jse.executeScript("window.scrollBy(0,250)", "");
// reasonable wait here
if(getProducts() == products){
//that it. nothing to scroll
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.