Skip to content

Selenium 4: Element screenshotting

08-10-2019 Roy de Kleijn

As from Selenium 4 it is possible to take screenshots of WebElements. The code below shows how you can do that.

@Test
public void elementScreenShot() throws InterruptedException, IOException {
   driver.get("https://www.github.com");

   WebElement svg = driver.findElement(By.id("Layer_1"));

   File file = svg.getScreenshotAs(OutputType.FILE);
   FileUtils.copyFile(file, new File("target/screenshot/element-screenshot.png"));

   // Added this sleep for demo purpose only
   Thread.sleep(15000);
}

Line 7 demonstrates how to take a screenshot of a WebElement.

If you want to perform screenshot comparisons, I recommend you take a look at Ashot API.

Delen:
Roy de Kleijn

Test Automation Expert

Met meer dan 15 jaar ervaring in software testing en test automation helpt Roy teams om hun testprocessen te verbeteren.

Wil je meer leren?

Bekijk onze trainingen en breng je test automation skills naar het volgende niveau.