question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[🐛 Bug]: Wrong screenshot is generated for elements within iframes

See original GitHub issue

What happened?

I am trying to take a screenshot of an element within an iframe using both TakesScreenshot and AShot classes upon switching to that certain iframe and I receive a wrong screenshot which is not of that element.

How can we reproduce the issue?

1. Have either Chrome 98.0.4758.82 or Microsoft Edge 98.0.1108.43 installed
2. Execute the following code on any website that has elements within an iframe (used http://demo.automationtesting.in/Frames.html for this test):

TakesScreenshot:
String currentUserDirectory = System.getProperty("user.home");
    File screenshot = ((TakesScreenshot) getWebDriver()).getScreenshotAs(OutputType.FILE);
    BufferedImage fullImg = ImageIO.read(screenshot);
    
    Point point = element.getLocation();
    
    setElementWidth(element.getSize().getWidth());
    setElementHeight(element.getSize().getHeight());
    
    BufferedImage elementScreenshot = fullImg.getSubimage(point.getX(), point.getY(), elementWidth, elementHeight);
    ImageIO.write(elementScreenshot, "png", screenshot);
    
    File screenshotPath = new File(currentUserDirectory + "\\Desktop\\" + " " + screenshotName);
    FileUtils.copyFile(screenshot, screenshotPath);

Test class method:

TakeScreenshot takeScreenshot = new TakeScreenshot(getWebDriver());
    
    TakeScreenshot takeScreenshot = new TakeScreenshot(getWebDriver());

    Thread.sleep(2000);
    getWebDriver().switchTo().frame(singleFrame);
    testElement.sendKeys("test");
    takeScreenshot.elementScreenshot(testElement, "TestElementTakesScreenshot.png"); // TakesScreenshot method

Relevant log output

No issues are received in the console.

Operating System

Windows 11

Selenium version

Java 4.1.2

What are the browser(s) and version(s) where you see this issue?

Chrome 98.0.4758.82 & Microsoft Edge 98.0.1108.43

What are the browser driver(s) and version(s) where you see this issue?

ChromeDriver 98.0.4758.80 & EdgeDriver 98.0.1108.43

Are you using Selenium Grid?

N/A

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
titusfortnercommented, Feb 10, 2022

You can just use the method on the element itself:

        driver.get("http://demo.automationtesting.in/Frames.html");
        driver.switchTo().frame("singleframe");

        WebElement input = driver.findElement(By.tagName("input"));
        input.sendKeys("test");

        byte[] screenshotAs = input.getScreenshotAs(OutputType.BYTES);
        Path screenshot = Paths.get("path/to/TestElementTakesScreenshot.png");
        Files.write(screenshot, screenshotAs);
0reactions
github-actions[bot]commented, Mar 13, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to take screenshot in Iframe - angular - Stack Overflow
I have created an example stackblitz demo here. Please help me, what I am doing wrong here. What is the other way to...
Read more >
The ultimate guide to iframes - LogRocket Blog
Not a fan of iframes? This post provides an overview of the tag's best features, shows you how to use them, and how...
Read more >
[New Plugin]: Screenshot Element Pro - Bubble Forum
We are excited to announce the launch of the Bubble plugin, Screenshot Element Pro! FEATURES: Generate screenshot of an Element by its ID ......
Read more >
Working with iframes — Firefox Source Docs documentation
Note that the button is only displayed if the page includes iframes! Screenshot showing how to set an iframe as the target of...
Read more >
Advanced iFrame – WordPress plugin
Include content the way YOU like in an iframe that can hide and modify elements, does auto height, forward parameters and does many,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found