How to take screenshot in Selenium WebDriver

0 votes
Does anyone have any idea, if it's possible to take a screenshot using WebDriver?
Apr 7, 2018 in Selenium by Martin
• 4,340 points
939 views

1 answer to this question.

0 votes

In Java, it can be done like this: 

WebDriver driver = new FirefoxDriver();
driver.get("Your-URL");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("yourPath.png"));
answered Apr 7, 2018 by nsv999
• 5,510 points

//for maven-selenium automation setup

//use this if FilleUtils is not working

//add this in pom.xml file

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

//on your script use this code below

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
Files.copy(scrFile, new File("yourPath.png"));

@Msizi, thanks for this code, but why we have to add Dependencies to the POM.xml file?
Because "FileUtils" class keep giving an error so i opted for the "Files" class which required the "commons-io" dependency to be added
Okay, thanks Msizi for explaining the needful.

Related Questions In Selenium

0 votes
1 answer

How to take screenshot of a frame using Selenium WebDriver?

you can use the below code: import java.awt.image.BufferedImage; import ...READ MORE

answered Jun 20, 2018 in Selenium by Meci Matt
• 9,460 points
1,563 views
0 votes
1 answer

How to take Screenshot of a webpage in Selenium with Ruby

Hi Sunita, you can use following code ...READ MORE

answered Aug 26, 2019 in Selenium by Anvi
• 14,110 points
300 views
0 votes
1 answer
0 votes
2 answers

Can anyone help me that how to run Selenium WebDriver test cases in Chrome?

You first need to download chrome driver ...READ MORE

answered Aug 26, 2019 in Selenium by Abha
• 27,930 points
723 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
3,569 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
4,574 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
2,619 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2 in Selenium by Sri
• 2,970 points
2,913 views
0 votes
4 answers

How to specify "ENTER" button functionality in Selenium WebDriver code?

using OpenQA.Selenium.Interactions; Actions builder = new Actions(driver); ...READ MORE

answered Feb 12, 2019 in Selenium by anonymous
40,933 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,510 points
4,376 views