Skip to content

Instantly share code, notes, and snippets.

@ShamaUgale
Last active September 24, 2020 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShamaUgale/071c8314e5a80502aedd8e2f3f2efc90 to your computer and use it in GitHub Desktop.
Save ShamaUgale/071c8314e5a80502aedd8e2f3f2efc90 to your computer and use it in GitHub Desktop.
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.io.File;
import java.io.IOException;
public class ScreenShotExamples {
final static String PROJECT_PATH = System.getProperty("user.dir");
public static void main(String[] args) throws IOException {
WebDriver driver = null;
System.setProperty("webdriver.chrome.driver", PROJECT_PATH+ "/src/main/resources/chromedriver");
driver = new ChromeDriver();
driver.manage().window().fullscreen();
driver.get("https://www.google.com");
// take screenshot of an element
WebElement logo= driver.findElement(By.id("hplogo"));
File file=logo.getScreenshotAs(OutputType.FILE);
File destFile =new File("logo.png");
FileUtils.copyFile(file,destFile);
System.setProperty("webdriver.gecko.driver", PROJECT_PATH+ "/src/main/resources/geckodriver");
FirefoxDriver firefoxDriver = new FirefoxDriver();
firefoxDriver.get("https://www.youtube.com");
// take full page screenshot - available for firefox driver
File src = firefoxDriver.getFullPageScreenshotAs(OutputType.FILE);
File destFile1 =new File("youtubeLandingPage.png");
FileUtils.copyFile(src,destFile1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment