Skip to content
Discussion options

You must be logged in to vote

I believe it would be more reliable to get element screenshot instead of trying to cut it off from the display screenshot. Example code:

        WebElement element = driver.findElement(By.id("my-element-id"));

        // Get element screenshot as PNG bytes
        byte[] pngBytes = element.getScreenshotAs(OutputType.BYTES);

        // Save PNG bytes to file
        try (FileOutputStream fos = new FileOutputStream("element.png")) {
            fos.write(pngBytes);
        }

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by chinenual
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants