In this video I have shown how to take screenshot of specific element only. It will not capture complete webpage instead it will only take particular element image. This is very useful in validations. If element is not visible on screen at the very first load it will scroll down and find that specific element and take the screenshot.
https://youtu.be/D17eYz8FVvg
https://youtu.be/4-Whn5BBV_c
https://youtu.be/aWgx76vA0qA
https://youtu.be/FoEexNif2As
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Details:
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.coordinates.WebDriverCoordsProvider;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class captureScreenShot {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "Path of chromedriver\\chromedriver.exe");
ChromeOptions op = new ChromeOptions();
op.setBinary("Path of Chrome exe\\chrome.exe");
op.addArguments("--remote-allow-origins=*");
WebDriver driver = new ChromeDriver(op);
driver.get("https://programmerworld.co/");
WebElement specificElement = driver.findElement(By.xpath("//img[@class='custom-logo']"));
Screenshot scrShot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(500)).coordsProvider( new WebDriverCoordsProvider()).takeScreenshot(driver, specificElement);
ImageIO.write(scrShot.getImage(), "jpg", new File("Directory Path To Save Screenshot\\ScreenshotName.jpg"));
}
}