Selenium - complete ajax loading autoscroll to bottom of page(Selenium - 完整的ajax加载自动滚动到页面底部)
问题描述
我有一个网页,当您滚动到底部时,它会通过 ajax 加载更多结果.您可以在它完成之前对其进行多次迭代.有点像 facebook 的做法.
I have a webpage where when you scroll to the bottom it then loads more results via ajax. You can do several iterations of this before it completes. Bit like what facebook does.
我正在尝试编写一个 selenium 脚本以一直到页面末尾,直到它完成.
I am trying to write a selenium script to keep going to the end of the page until it completes.
类似这样的一半完成它..我只是不知道如何确定页面是否在底部 - 所以我可以将它放在某种循环中?
Something like this sort of half completes it.. I just don't know how to determine if page is at the bottom - so i can put it inside a loop of some sort?
我的尝试
By selBy = By.tagName("body");
driver.findElement(selBy).sendKeys(Keys.PAGE_DOWN);
System.out.println("Sleeping... wleepy");
Thread.sleep(5000);
driver.findElement(selBy).sendKeys(Keys.PAGE_DOWN);
System.out.println("Sleeping... wleepy1");
Thread.sleep(3000);
//etc...
会像这样吗?
hasScroll() 不是真正的方法.我把它放在那里是为了展示我想要实现的目标
Could Look like this?
hasScroll() isnt a real method. I put that there to demonstrate what im trying to achieve
while (driver.findElement(By.tagName("body")).hasScroll()) {
driver.findElement(selBy).sendKeys(Keys.PAGE_DOWN);
System.out.println("Sleeping... wleepy");
Thread.sleep(2000);
}
推荐答案
试试这个方法:
//Get total height
By selBy = By.tagName("body");
int initialHeight = driver.findElement(selBy).getSize().getHeight();
int currentHeight = 0;
while(initialHeight != currentHeight){
initialHeight = driver.findElement(selBy).getSize().getHeight();
//Scroll to bottom
((JavascriptExecutor) driver).executeScript("scroll(0," + initialHeight + ");");
System.out.println("Sleeping... wleepy");
Thread.sleep(2000);
currentHeight = driver.findElement(selBy).getSize().getHeight();
}
希望有帮助!
这篇关于Selenium - 完整的ajax加载自动滚动到页面底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Selenium - 完整的ajax加载自动滚动到页面底部


基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 降序排序:Java Map 2022-01-01