VIGNESH
VIGNESH

Reputation: 69

how to take full page screenshot in chrome using selenium

In selenium tool, how to take full page screenshot and how to scroll the page? If we use this code,

JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");

it directly move to the bottom of the page.

Upvotes: 0

Views: 3613

Answers (3)

Jatin gangwani
Jatin gangwani

Reputation: 11

System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe"); 

ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);   
String baseUrl = "https://www.google.co.in";

driver.get(baseUrl);        
String fullscreen =Keys.chord(Keys.F11);
driver.findElement(By.cssSelector("body")).sendKeys(fullscreen);

TakesScreenshot scrShot =((TakesScreenshot)driver);  
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);   
File DestFile=new File("F://test.png");  
FileUtils.copyFile(SrcFile, DestFile);   
driver.close();

Upvotes: 1

Rabia Asif
Rabia Asif

Reputation: 298

Use the jar file of shutterbug in selenium. Here you can download the file : http://book2s.com/java/jar/s/selenium-shutterbug/download-selenium-shutterbug-0.4.html

And here is the command that will be placed in your code:

Shutterbug.shootPage(driver, ScrollStrategy.BOTH_DIRECTIONS).save("path to save image");

You dont need to handle any scrolls this is going to automatically capture full page screenshot

Upvotes: 0

Saurabh Verma
Saurabh Verma

Reputation: 668

For this, we need to download the ashot.jar file and add to the project along with the selenium jar files

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;

import javax.imageio.ImageIO;
import java.io.File;

public class TakeFullPageScreenShot
{
   public static void main(String args[]) throws Exception
   {
       System.setProperty("webdriver.gecko.driver",     "D:\\Selenium\\geckodriver.exe");
   WebDriver driver = new FirefoxDriver();

   driver.get("http://automationtesting.in/");
   Thread.sleep(2000);

   Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
   ImageIO.write(screenshot.getImage(),"PNG",new                              
File(System.getProperty("user.dir") +"/ErrorScreenshots/FullPageScreenshot.png"));
 }
}

It will scroll the page to the end and captures the screenshot. And we can control the scroll speed using the viewportPasting method.

hope that helps

Upvotes: 0

Related Questions