Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 15, 2019

How to Capture WebElement Screenshot in Selenium Webdriver using selenium 4




In this blog, we will learn how to capture webelement screenshot using selenium. Till Selenium 3 we have an option that to take the screenshot for an entire webpage. From selenium 4 onwards they have introduced an option that to capture a webelement. Previously, using some third party libraries like Ashot, shutterbug to capture the webelemet or webpage.




Pre-requisites:

  1. Add the selenium-4-alpha.jar file in your project or add the corresponding dependency is yours is a Maven project
  2. in your class file, inspect the target element and store it in a webelement.
  3. Now, use the regular screenshot logic method and store the value in a folder using Filehandler method.
  4. Instead of driver, use the webelement reference variable to capture the screenshot of the target element instead of full webpage.
package Basics;

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.io.FileHandler;

public class CaptureWebelementScreenshot {

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "E:\\Selenium-Softwares\\chromedriver.exe");
//System.setProperty("webdriver.gecko.driver", "E:\\Selenium-Softwares\\geckodriver.exe");
driver=new ChromeDriver();
driver.get("http://demo.automationtesting.in/Register.html");
Thread.sleep(3000);
WebElement element = driver.findElement(By.id("imagetrgt"));

File source = ((TakesScreenshot)element).getScreenshotAs(OutputType.FILE);
FileHandler.copy(source, new File("./screenshots/element.png"));
Thread.sleep(3000);
driver.quit();

}

}

Please watch the Youtube video for better understanding.



Share this post: on Twitter on Facebook on Google+

How to SWAP Two Numbers in Java Without Temp variable or Without Third variable

Related Posts

avoid Switch to window

Selenium - Java

How to Avoid Switch To Window in Selenium WebDriver

webdrivermanager

Selenium - Java

No More Driver EXE files for Selenium Execution – Instead Use WebDriverManager

HANDLE TEXTBOXES IN SELENIUM

Selenium - Java

Handling Textboxes in Selenium

JAVASCRIPTEXECUTOR IN SELENIUM

Selenium - Java

JavaScriptExecutor in Selenium

ELEMENT NOT VISIBLE EXCEPTION

Selenium - C#, Selenium - Java

Selenium ElementNotVisibleException

NOSUCHWINDOW EXCEPTIONS

Selenium - C#, Selenium - Java

Selenium NoSuchWindowException

NOSUCHFRAME EXCEPTIONS

Selenium - C#, Selenium - Java

Selenium NoSuchFrameException

NOSUCHELEMENT EXCEPTION

Selenium - C#, Selenium - Java

Selenium NoSuchElementException

SELENIUM WEBDRIVER EXCEPTIONS

Selenium - C#, Selenium - Java

Selenium WebDriver Exceptions

HANDLING BASIC ELEMENTS IN SELENIUM

Selenium - C#, Selenium - Java

Handling Basic Elements in Selenium

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2023