Disclaimer: MashTips is supported by its audience. When you purchase through links on our site, we may earn an affiliate commission at no extra cost to you.

Home Code How to Take Screenshot and save to Local Folder using Java and...

How to Take Screenshot and save to Local Folder using Java and Selenium Webriver

This is to take a screenshot of any web page and save the image into a local folder. This is based on Selenium Webdriver and currently supporting Firefox, Internet Explorer, Chrome, and Safari. The image can save to local folder with the current time as the image name.

Take Screenshot and save to Local Folder using Java and Selenium Webriver

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

public class ImageSave {
String imageName = System.currentTimeMillis() + ".png";

public void addScreenshot(WebDriver driver) throws IOException,
ClassNotFoundException, InterruptedException {
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("C://screenshot//" + imageName));
}
}

1 COMMENT

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Free Newsletter

Related Articles

%d bloggers like this: