How to take a Screenshot in Selenium WebDriver - Codoid
Select Page
Codoid Blog

How to take a Screenshot in Selenium WebDriver

Irrespective of the business that your client is in, marketing is an integral part of their operations. In fact, in this day and age of social.

Screenshots are important to analyse the execution failures. It helps an automation tester to analyse what went wrong during execution. As one of the best automation testing services companies, we have also faced several challenges when troubleshooting test automation failures. Screenshot is a great reference document to recheck how the functionalities behaved previously and to identify false negatives from script failures.

In this blog article, we would like to share how to take screenshot in Selenium WebDriver using various commands.

Full Page Screenshot

In Selenium 4, you can take full page screenshot. This feature is very helpful to investigate failures in detail.

File file = ((FirefoxDriver)driver).getFullPageScreenshotAs(OutputType.FILE);
  

Note: As of now, full page screenshot works only for Firefox driver.

Element Screenshot

Sometimes you may need screenshot only for a specific element instead of capturing the entire page.

driver.findElement(By.id("txt1")).getScreenshotAs(OutputType.FILE)
  

Full Page Screenshot using aShot

aShot is a WebDriver Screenshot utility. It takes a screenshot of the WebElement on different platforms (i.e. desktop browsers, iOS Simulator Mobile Safari, Android Emulator Browser).

Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(fpScreenshot.getImage(),"PNG",new File("D:///FullPageScreenshot.png"));
  

Viewport Screenshot

File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
  

Written By

Submit a Comment

Your email address will not be published. Required fields are marked *


Screenshots are important to analyse the execution failures. It helps an automation tester to analyse what went wrong during execution. As one of the best automation testing services companies, we have also faced several challenges when troubleshooting test automation failures. Screenshot is a great reference document to recheck how the functionalities behaved previously and to identify false negatives from script failures.

In this blog article, we would like to share how to take screenshot in Selenium WebDriver using various commands.

Full Page Screenshot

In Selenium 4, you can take full page screenshot. This feature is very helpful to investigate failures in detail.

File file = ((FirefoxDriver)driver).getFullPageScreenshotAs(OutputType.FILE);
  

Note: As of now, full page screenshot works only for Firefox driver.

Element Screenshot

Sometimes you may need screenshot only for a specific element instead of capturing the entire page.

driver.findElement(By.id("txt1")).getScreenshotAs(OutputType.FILE)
  

Full Page Screenshot using aShot

aShot is a WebDriver Screenshot utility. It takes a screenshot of the WebElement on different platforms (i.e. desktop browsers, iOS Simulator Mobile Safari, Android Emulator Browser).

Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(fpScreenshot.getImage(),"PNG",new File("D:///FullPageScreenshot.png"));
  

Viewport Screenshot

File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);