Objective: Scroll in equivalent intervals to take screenshot of an entire page using Java and JSExecutor for Selenium WebDriver.
Issue: The approach I've implemented below works, however, I end up having 2-3 extra screenshots at the end of the web page - I want to avoid these redundant screenshots.
Scrolling method is below:
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);
}
}
Screenshot method is below:
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);
}
Following variables are defined as static:
static int pageHeight = 0;
static int count = 0;
static WebDriver driver;
I understand there's no implementation currently to capture screen of an entire web page using Selenium. Any help to resolve my logic above would be greatly appreciated.