Examples on getScreenShotAs() Method
Note: You need to create a maven project and you need add Selenium 4.0.0-alpha-3 dependency.
UI WebElement screenshot using getScreenshotAs
UI WebElement screenshot using getScreenshotAs
import java.io.File;
import
java.io.IOException;
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
io.github.bonigarcia.wdm.WebDriverManager;
public class screenShotExp1
{
public static void main(String[] args) throws IOException {
WebDriverManager.chromedriver().setup();
WebDriver
driver=new ChromeDriver();
driver.get("https://www.facebook.com/");
WebElement siginObj=driver.findElement(By.xpath("//button[@name='websubmit']"));
File
objFile=siginObj.getScreenshotAs(OutputType.FILE);
//want to copy
WebElement Screenshot to particular folder
FileUtils.copyFile(objFile, new File("C:\\Temp\\SiginUpButton.png"));
}
}
UI WebElement screenshot using TakeScreenShot Class
import java.io.File;
import
java.io.IOException;
import
org.apache.commons.io.FileUtils;
import
org.openqa.selenium.By;
import
org.openqa.selenium.OutputType;
import
org.openqa.selenium.TakesScreenshot;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.chrome.ChromeDriver;
import
io.github.bonigarcia.wdm.WebDriverManager;
public class
screenShotusingTakeScreen {
public static void main(String[] args) throws IOException {
WebDriverManager.chromedriver().setup();
WebDriver
driver=new ChromeDriver();
driver.get("https://www.facebook.com/");
WebElement
siginObj=driver.findElement(By.xpath("//button[@name='websubmit']"));
TakesScreenshot
siginupbtn=((TakesScreenshot)siginObj);
File
objFile=siginupbtn.getScreenshotAs(OutputType.FILE);
//i want to
copy objfileScreenshot to particular folder
FileUtils.copyFile(objFile, new File("C:\\Temp\\FBSiginUpButton.png"));
}
}
Take a ScreenShot – Section of the Page using getScreenshotAs
import java.io.File;
import
java.io.IOException;
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
io.github.bonigarcia.wdm.WebDriverManager;
public class
sectionofScreenShot {
public static void main(String[] args) throws IOException {
WebDriverManager.chromedriver().setup();
WebDriver
driver=new ChromeDriver();
driver.get("https://www.facebook.com/");
WebElement
siginObj=driver.findElement(By.xpath("//table[@role='presentation']"));
File
objFile=siginObj.getScreenshotAs(OutputType.FILE);
//i want to
copy objfileScreenshot to particular folder
FileUtils.copyFile(objFile, new File("C:\\Temp\\sectionofObjs.png"));
}
}
Note: You should give the parent object Locator of the child objects.
Take
a Screen Shot of a Full Web Page using getScreenShotAs()
import java.io.File;
import
java.io.IOException;
import
org.apache.commons.io.FileUtils;
import
org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import
org.openqa.selenium.chrome.ChromeDriver;
import
io.github.bonigarcia.wdm.WebDriverManager;
public class
fullScreenShotExp {
public static void main(String[] args) throws IOException {
WebDriverManager.chromedriver().setup();
WebDriver driver=new ChromeDriver();
driver.get("https://www.facebook.com/");
driver.manage().window().maximize();
File
fullscreen=((ChromeDriver)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(fullscreen, new File("C:\\Temp\\fullscreen.png"));
}
}
Comments
Post a Comment