How to capture a screenshot of a particular element in Selenium 4.0?

Software TestingAutomation TestingSelenium Web Driver

We can capture screenshots of a particular element in Selenium 4.0 with the help of getScreenshotAs(OutputType.File) method where the OutputType tells about the output type of the screenshot.

Example

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
public class ScreenshotsElement{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
"C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://www.tutorialspoint.com/tutor_connect/index.php";
      WebElement drpdwn = driver.findElement(By.xpath("//select[@name=’selType’]"));
      // capture screenshot with getScreenshotAs() of the dropdown
      File f = drpdwn.getScreenshotAs(OutputType.FILE);
      FileUtils.copyFile(f, new File(“screenshots.png”));
      driver.quit();
   }
}
raja
Published on 11-Jun-2020 15:26:56
Advertisements