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,220 points
612 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,090 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,420 points
672 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 in Selenium by Anvi
• 12,860 points
11 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 in Selenium by Abha
• 23,030 points
250 views
0 votes
1 answer

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,090 points
588 views
0 votes
1 answer
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,130 points
898 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
574 views
0 votes
3 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 in Selenium by anonymous
8,964 views
0 votes
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,090 points
1,993 views