As a beginner like me, you will be very keen to take screenshots of a specific page. Here is a simple script for taking a screenshot of any URL. I have used "http://www.flipkart.com/" as my URL.
  1. package login;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.concurrent.TimeUnit;
  5. import org.apache.commons.io.FileUtils;
  6. import org.openqa.selenium.OutputType;
  7. import org.openqa.selenium.TakesScreenshot;
  8. import org.openqa.selenium.WebDriver;
  9. import org.openqa.selenium.firefox.FirefoxDriver;
  10. public class Screen_shot
  11. {
  12. public static void main(String[] args) throws IOException
  13. {
  14. // TODO Auto-generated method stub
  15. // Initialize WebDriver
  16. WebDriver driver = new FirefoxDriver();
  17. // Wait For Page To Load
  18. driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
  19. // Go to URL
  20. driver.get("http://www.flipkart.com/");
  21. // Maximize Window
  22. driver.manage().window().maximize();
  23. // Take ScreenShot
  24. File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
  25. FileUtils.copyFile(scrFile, new File("D:\\selenium\\screenshot1.png"), true);
  26. // Close Driver
  27. driver.quit();
  28. }
  29. }
The following are a few points with images.










  • For the preceding image you need to Import this package only [ 'FileUtlis' (org.apache.commons.io) ]
  • After completion of the script, the PNG file will be stored in your given location. I have given [ "D:\\selenium\\screenshot1.png" ]. Here is the screen shot below.
  • The file has been saved in the "png" format as specified in the script. We can save the image in various other file types, like PNG, JPEG, GIF or BMP.
I hope this helps Beginners like me. :)