S
S
Stanislav Kudelko2017-04-23 14:46:38
Java
Stanislav Kudelko, 2017-04-23 14:46:38

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:
BfvSI.png
Here is the element that needs to be screened:
8EJYA.png
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"));

After taking the element, an exception is thrown :
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)

I still can't fix it. By the way, the resulting values ​​look like this:
x = 459 y = 1034 widt = 130 height = 60
P/s Thanks in advance for your answer!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question