如何使用Java在selenium中滚动网页?
我正在尝试滚动使用React JS构建的页面。
即使在使用js.executeScript("scroll(0, 250);");
无效后,页面也不会滚动
并尝试滚动该方法,直到某个元素可见
你可以在下面搜索框中键入要查询的问题!
我正在尝试滚动使用React JS构建的页面。
即使在使用js.executeScript("scroll(0, 250);");
无效后,页面也不会滚动
并尝试滚动该方法,直到某个元素可见
正如你提到的,页面自动刷新2次,然后加载。可能存在问题,因为您试图在首次自动刷新后滚动页面。在这两次自动刷新之间会有微秒的延迟,所以selenium正在尝试滚动页面,但在此之前,站点会自动刷新两次。我可以要求您在使用JavaScript滚动方法之前设置检查点,并进行调试以确保它是否实际工作。如果它真的工作,那么当然你需要执行任何行动后,2自动刷新发生
# 1 楼答案
您能试试这个吗?我希望您的代码中缺少全局变量
window
# 2 楼答案
您可以使用我编写的以下方法滚动到特定元素。您只需要传递驱动程序对象