How to take full page Screenshot of a scrollable webpage using Selenium WebDriver with Java?
Solution 1
Please Find the below code, you can scroll and take screenshots as many as you want. Note the elements
Webelement. Store them and scroll relatively.
You can Scroll depending upon how many screenshots you want.
driver.get("http://www.bbc.com");
driver.manage().window().maximize();
System.out.println(driver.getTitle());
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:/chromedriver/scr3.png"));
WebElement elements = driver.findElement(By.xpath(".//*[@id='page']/section[6]/div/div/div[1]/ul/li[3]/div/div[2]/h3/a"));
Thread.sleep(3000L);
JavascriptExecutor js = (JavascriptExecutor) driver;
int yPosition = elements.getLocation().getY();
js.executeScript("window.scroll (0, " + yPosition + ") ");
Thread.sleep(3000L);
File scrFile1 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile1, new File("D:/chromedriver/scr4.png"));
driver.close();
Solution 2
You can't do this using merely selenium. You need other tool to perform your task. Follow the link and see my answer: Screen shot issue in selenium webdriver
Hope, it may help you.
Solution 3
You Can Try This Method. Hope It Will Be Useful:-
public static void takeFullPageScreenShot(WebDriver driver) throws IOException {
JavascriptExecutor jsExec = (JavascriptExecutor)driver;
jsExec.executeScript("window.scrollTo(0, 0);"); //Scroll To Top
Long innerHeight = (Long) jsExec.executeScript("return window.innerHeight;");
Long scroll = innerHeight;
Long scrollHeight = (Long) jsExec.executeScript("return document.body.scrollHeight;");
scrollHeight = scrollHeight + scroll;
do{
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//Unique File Name For Each Screenshot
File destination = new File("E://screenshots//"+String.join("_",
LocalDateTime.now().toString().split("[^A-Za-z0-9]"))+".jpg");
FileUtils.copyFile(screenshot, destination));
jsExec.executeScript("window.scrollTo(0, "+innerHeight+");");
innerHeight = innerHeight + scroll;
}while(scrollHeight >= innerHeight);
}
Or,
File screenshot =
driver.findElement(By.tagName("body")).getScreenshotAs(OutputType.FILE);
File destination = new File("E://screenshots//"+String.join("_",
LocalDateTime.now().toString().split("[^A-Za-z0-9]"))+".jpg");
FileUtils.copyFile(screenshot, destination));
Solution 4
To take screenshot of a complete webpage, we have to use a third party utility called 'aShot'. aShot is a WebDriver Schreenshot Utility with which we can take screenshot of the entire webpage & also individual WebElement. To do this, we have to download the aShot jar file & add to our project along with Selenium jar files.
Solution 5
Using Shutterbug, short and sweet:
public byte[] shootPage() throws IOException {
BufferedImage image = Shutterbug.shootPage(driver, ScrollStrategy.WHOLE_PAGE).getImage();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image, "png", outputStream);
return outputStream.toByteArray();
}
Comments
-
Sarthak Srivastava over 2 years
Existing code which takes screenshot of only visible screen. I am using the
Chromedriver
.System.setProperty("webdriver.chrome.driver", "D:/chromedriver/chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://www.bbc.com"); driver.manage().window().maximize(); System.out.println(driver.getTitle()); File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile, new File("D:/chromedriver/scr3.png")); driver.close();