【发布时间】:2019-07-07 16:38:15
【问题描述】:
目标:使用 Java 和 JSExecutor for Selenium WebDriver 以相等的间隔滚动以截取整个页面。
问题:我在下面实现的方法有效,但是,我最终在结束时有 2-3 个额外的屏幕截图网页 - 我想避免这些多余的屏幕截图。
滚动方式如下:
public static void pageScrollable() throws InterruptedException, IOException {
JavascriptExecutor jse = (JavascriptExecutor) driver;
//Find page height
pageHeight = ((Number) jse.executeScript("return document.body.scrollHeight")).intValue();
//Find current browser dimensions and isolate its height
Dimension d = driver.manage().window().getSize();
int browserSize = d.getHeight();
int currentHeight = 0;
System.out.println("Current scroll at: " + currentHeight);
System.out.println("Page height is: " + pageHeight + "\n");
//Scrolling logic
while(pageHeight>=currentHeight) {
jse.executeScript("window.scrollBy(0,"+currentHeight+")", "");
screenShot();
currentHeight+=browserSize;
System.out.println("Current scroll now at: " + currentHeight);
}
}
截图方法如下:
public static void screenShot() throws IOException, InterruptedException {
File screenShot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenShot, new File("C:\\FilePath\\Screen " + count + ".png"));
count++;
System.out.println("Current screenshot count: " + count);
}
以下变量被定义为静态:
static int pageHeight = 0;
static int count = 0;
static WebDriver driver;
我知道目前没有使用 Selenium 捕获整个网页的屏幕的实现。任何帮助解决我的上述逻辑将不胜感激。
【问题讨论】:
标签: selenium selenium-webdriver