Import Java.awt.Rectangle;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import Javax.imageio.ImageIO;
Import Org.apache.commons.io.FileUtils;
Import Org.openqa.selenium.OutputType;
Import Org.openqa.selenium.Point;
Import Org.openqa.selenium.TakesScreenshot;
Import Org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
public class Screenshot {
public void Screenshotforelement (Webdriver driver,webelement element, String path,int X,int y) throws Interruptedexcepti on {
Capture a picture of the entire page
File Scrfile = ((takesscreenshot) driver). Getscreenshotas (Outputtype.file);
try {
Gets the position object in the frame where the element is located
Point P = element.getlocation ();
Gets the width and height of the element
int width = element.getsize (). GetWidth ();
int height = element.getsize (). GetHeight ();
Rectangle Image Object
Rectangle rect = new Rectangle (width, height);
BufferedImage img = imageio.read (scrfile);
X, y to add the left margin of the current frame, top margin
BufferedImage dest = Img.getsubimage (P.getx () +x, p.gety () +y,rect.width, rect.height);
Imageio.write (dest, "PNG", scrfile);
Fileutils.copyfile (Scrfile, new File (path));
} catch (IOException e) {
E.printstacktrace ();
}
}
}
How to implement regions in Webdriver and how to intercept pictures in a frame