我正在尝试自动化网页,我需要一直滚动到页面底部并单击页脚。但是,我自动化的网页有无限滚动。对此有何帮助?
public class practiceNG
{
WebDriver driver;
@BeforeTest
public void start()
{
System.setProperty("webdriver.chrome.driver", "Y:\\Selenium\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://the-internet.herokuapp.com/");
}
@Test (priority = 3)
public void infiniteScroll() throws InterruptedException
{
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,200)", "");
driver.findElement(By.xpath(".//[@id=\'content\']/ul/li[23]/a")).click();
Thread.sleep(1000);
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
driver.findElement(By.xpath("//*[@id=\"page-footer\"]/div/div/a")).click();
}
@AfterTest
public void close()
{
driver.quit();
}
}
答案 0 :(得分:1)
您可以使用以下代码
while(footerIsNotPresent()){
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollTo(0, document.body.scrollHeight);");
}
您可以将footerIsNotPresent()替换为您自己的代码,以检查是否已到达页面底部。它将继续滚动直到页脚不存在。