Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

When taking a screenshot using Selenium Webdriver on windows with python, the screenshot is saved directly to the path of the program, is there a way to save the .png file to a specific directory?

1 Answer

0 votes
by (62.9k points)

Test cases may fail while executing test cases. While we are executing the test cases manually we just take a screenshot and place in a result repository. This can be done by using Selenium WebDriver. 

 The following are the scenarios when we may need to capture a screenshot using Selenium WebDriver.

i. Application issues

ii. Assertion Failure

iii. Difficulty to find Web elements on the web page

iv. Timeout to find Web elements on the web page

 Syntax to capture and save the screenshot.

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

Syntax to store it in our local drive

FileUtils.copyFile(screenshotFile, new File("filename_with_path"));

For example:

FileUtils.copyFile(screenshotFile, new FIle("D:\\screenshot.png"));

Below is the code that implements on how to capture a screenshot using Selenium WebDriver.

package intellipaat;




import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;


public class CaptureScreenshot {


public static void captureScreenMethod() throws Exception{

System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe");

WebDriver driver = new FirefoxDriver();



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

FileUtils.copyFile(screenshotFile, new File("D:\\intellipaat.png"));





Browse Categories