Skip to main content

You are here

Taking Screen Shot using Selenium WebDriver

The following code takes a screen shot of the website www.sojicity.com. First, the Firefox browser is opened, then the site is loaded. The screen shot is taken and stored in a folder on the C:logicandtricks. Ensure you edit the location for the screen shot file as you see fit on the line:

FileUtils.copyFile(screenshot, new File("c:\\logicandtricks\\screenshot.jpg"));

To run the program you need to have Selenium configured.

Have fun!

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TakeAScreenShot {

public static void main(String[] args) throws InterruptedException, IOException {
//Initialise firefox browser
WebDriver driver = new FirefoxDriver();

/* Next line makes the browser wait for 7 seconds before declaring it cant find an element.
Good for slow loading websites*/
driver.manage().timeouts().implicitlyWait(7,TimeUnit.SECONDS);
driver.get("http://www.sojicity.com/");

System.out.println("Taking Screen Shot");
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("c:\\logicandtricks\\screenshot.jpg"));

driver.quit();
}
}

Category: 

Language: 

0
No votes yet

Add new comment

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.

Disclaimer

Note that the codes and ideas expressed on this site are the authors personal views. The content of this website are the personal notes based on the learning experience of the author. The author will not be responsible for any error or damages resulting from you using the content of this website.

Premium Drupal Themes by Adaptivethemes