Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Screenshoot of elements in scrolled page doesn't work properly #844

Closed
vachix opened this issue Jun 2, 2021 · 4 comments · Fixed by #863
Closed

Screenshoot of elements in scrolled page doesn't work properly #844

vachix opened this issue Jun 2, 2021 · 4 comments · Fixed by #863
Assignees

Comments

@vachix
Copy link

vachix commented Jun 2, 2021

Hello, it looks like the problem is the Chrome DevTools Protocol for screenshot expects a position of element relative to the root document, but the dom.GetBoxModel() method returns a position relative to the viewport. Here is my quick fix works for me 0763802

What versions are you running?

github.com/chromedp/chromedp v0.7.3
Version 91.0.4472.77 (Official Build) (64-bit)
go version go1.16.4 windows/amd64

What did you do? Include clear steps.

var buf []byte
if err := chromedp.Run(ctx, chromedp.Screenshot([]cdp.NodeID{node.NodeID}, &buf, chromedp.ByNodeID)); err != nil {
	log.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(itemPath, `list_item.png`), buf, 0o644); err != nil {
	log.Fatal(err)
}

What did you expect to see?

Screenshoot of my element

What did you see instead?

Screenshot of another part of page

@ZekeLu
Copy link
Member

ZekeLu commented Jun 3, 2021

@vachix Thanks for the sharing! Would you like to send a PR?

Things to note:

  1. The Protocol Monitor shows that the Capture node screenshot command uses a javascript function to get the clip of the element (using the command Runtime.callFunctionOn):
    function () {
        const e = this.getBoundingClientRect(), t = this.ownerDocument.documentElement.getBoundingClientRect();
        return JSON.stringify({x: e.left - t.left, y: e.top - t.top, width: e.width, height: e.height, scale: 1})
    }
    I think we can do the same here.
  2. When the element is outside the viewport, "captureBeyondViewport":true is required. And the the Capture node screenshot command adds "fromSurface":true too. I don't know what this option for. Maybe we should add it too.
  3. We need a test case to cover it (I don't know how as of now. Maybe do something like this in go: https://github.com/puppeteer/puppeteer/blob/943477cc1eb4b129870142873b3554737d5ef252/test/golden-utils.js#L39).
  4. The screenshot function does not work properly when the target element is in an iframe. Maybe we should report it to Chromium.

It seems there is a lot to do. So it's okay if you don't want to work on it.

Thank you very much!

------8<------

Update: I found this document: Viewport bounds and scale for screenshots

@ZekeLu
Copy link
Member

ZekeLu commented Jun 15, 2021

@vachix I'm going to work on this issue. Thank you!

@ZekeLu ZekeLu self-assigned this Jun 15, 2021
ZekeLu added a commit to ZekeLu/chromedp that referenced this issue Jun 15, 2021
Before this change, it's relative to the viewport. So it will get
the wrong position if the page scrolls.

Fixes chromedp#844.
@ZekeLu
Copy link
Member

ZekeLu commented Jun 15, 2021

@vachix I have just pushed some commits to my forked repository (https://github.com/zekelu/chromedp/). Can you clone the repository and checkout the screenshot branch, then use the replace directive in go.mod to test whether this issue has been addressed? Thank you!

@ZekeLu
Copy link
Member

ZekeLu commented Jun 17, 2021

Status update (in #863):

  1. using the command Runtime.callFunctionOn to get the clip of the element: done
  2. parameters for Page.captureScreenshot:
    • "captureBeyondViewport":true: done
    • "fromSurface":true: it defaults to true, no change required
  3. https://github.com/orisano/pixelmatch is used to compare images.
  4. This issue has been reported by someone else: https://bugs.chromium.org/p/chromium/issues/detail?id=1111398

ZekeLu added a commit to ZekeLu/chromedp that referenced this issue Jun 26, 2021
Before this change, it's relative to the viewport. So it will get
the wrong position if the page scrolls.

Fixes chromedp#844.
ZekeLu added a commit to ZekeLu/chromedp that referenced this issue Jul 12, 2021
Before this change, it's relative to the viewport. So it will get
the wrong position if the page scrolls.

Fixes chromedp#844.
ZekeLu added a commit that referenced this issue Jul 12, 2021
Before this change, it's relative to the viewport. So it will get
the wrong position if the page scrolls.

Fixes #844.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants