LambdaTest Community

Find answers, support, and inspiration from other users

Questions
LambdaTest Community
Answered

How can I take a screenshot of a web page in Selenium? Can you share Java code examples?

Can anyone give me a valid tutorial on how can I take a screenshot of a web page in Selenium and also can anyone please share Java code examples.

Up Vote Down Vote 0 Votes
Posted 3 months ago

Answers


In order to capture screenshots of the entire screen using selenium web driver scripts, we can make use of AShot().

AShot() is a webdriver screenshot utility to capture entire page screenshot and is natively supported from Selenium 3 onwards. It provides the following features:

  • Helps capture entire screen and web element
  • Beautify screenshot
  • Provides screenshot comparison.

In order to take a screenshot of the entire screen, you need to add the jar into your project. You can download the jar from here http://central.maven.org/maven2/ru/yandex/qatools/ashot/ashot/1.5.3/ashot-1.5.3.jar

Once the jars are added into the project, all you need to do is mention the below lines of code when you intend to take the full-screen screenshot:

Screenshot screenshot=new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);

ImageIO.write(screenshot.getImage(),"PNG",new File("path of the file"));

In the code below, the Ashot method shooting strategy is used by setting the viewport to full screen and taking screenshots. This code snippet below goes to Airbnb India Stays and Tours page and takes a screenshot of the full view.

import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.Test;

import ru.yandex.qatools.ashot.AShot;

import ru.yandex.qatools.ashot.Screenshot;

import ru.yandex.qatools.ashot.shooting.ShootingStrategies;

public class EntireScreenshot {

public static void main(String[] args) {

// TODO Auto-generated method stub

WebDriver driver;

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

driver=new ChromeDriver();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.manage().window().maximize();

driver.get("Airbnb® | India - Vacation Rentals & Places to Stay");

//take screenshot of the entire page

Screenshot screenshot=new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);

try {

ImageIO.write(screenshot.getImage(),"PNG",new File("path of the file"));

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

driver.quit();

}

}
Up Vote Down Vote 0 Votes
Posted 3 months ago