Category: Snippets

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: 1234WebDriver 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"));