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.   
  11. public class Screen_shot  
  12. {  
  13.   
  14.     public static void main(String[] args) throws IOException  
  15.     {  
  16.         // TODO Auto-generated method stub  
  17.   
  18.         // Initialize WebDriver  
  19.         WebDriver driver = new FirefoxDriver();  
  20.         // Wait For Page To Load  
  21.   
  22.         driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);  
  23.         // Go to URL  
  24.         driver.get("http://www.flipkart.com/");  
  25.         // Maximize Window  
  26.         driver.manage().window().maximize();  
  27.         // Take ScreenShot  
  28.         File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);  
  29.         FileUtils.copyFile(scrFile, new File("D:\\selenium\\screenshot1.png"), true);  
  30.         // Close Driver  
  31.         driver.quit();  
  32.     }  
  33. }  
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. :)

Next Recommended Readings