How to take a screenshot with selenium

When we are working with Selenium framework it’s possible that we need to take a screenshot of the webpage loaded by the selenium webdriver. This is easy to do, you only need to cast the driver to “TakesScreenShot” class and call “getScreenshotAs”. Look the snippet and use it at your convenience:

1
2
3
4
WebDriver driver = new FirefoxDriver(); // or other
driver.get("http://www.yahoo.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("my_screenshot.png"));

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>