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,320 points
2,043 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,500 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
3,522 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,150 points
1,601 views
0 votes
1 answer

how to take screenshot in selenium

In order to capture a screenshot in ...READ MORE

answered Feb 3, 2022 in Selenium by Edureka
• 850 points
911 views
0 votes
1 answer
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,500 points
13,806 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,209 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,910 points
7,287 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 3, 2020 in Selenium by Sri
• 3,190 points
7,914 views
0 votes
6 answers

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

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

answered Feb 13, 2019 in Selenium by anonymous
96,310 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,500 points
8,430 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP