Answer the question
In order to leave comments, you need to log in
How to take a screenshot of an element correctly?
Hello. On the site, it was necessary to screenshot the captcha, and from the page it was her. Here is the page:
Here is the element that needs to be screened:
The code that I use to take the screen looks like this:
WebElement element = driver.findElement(By.cssSelector("#wizard > div > div.ofm-forms.autoba-addlot.step.step3 > div.ofm-forms-captcha > div"));
File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
int ImageWidth = element.getSize().getWidth();
int ImageHeight = element.getSize().getHeight();
Point point = element.getLocation();
int xcord = point.getX();
int ycord = point.getY();
BufferedImage img = ImageIO.read(screen);
BufferedImage dest = img.getSubimage(xcord, ycord, ImageWidth, ImageHeight);
ImageIO.write(dest, "png", screen);
FileUtils.copyFile(screen, new File("captcha.png"));
Exception in thread "main" java.awt.image.RasterFormatException: (y + height) is outside of Raster
at sun.awt.image.ByteInterleavedRaster.createWritableChild(ByteInterleavedRaster.java:1248)
at java.awt.image.BufferedImage.getSubimage(BufferedImage.java:1202)
at Main.upBlank(Main.java:199)
at Main.main(Main.java:58)
x = 459 y = 1034 widt = 130 height = 60
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question