sindresorhus / capture-website

Capture screenshots of websites
MIT License
1.93k stars 137 forks source link

Capturing elements inside `#shadow-root` #100

Closed schardev closed 2 years ago

schardev commented 2 years ago

I tried capturing an element inside the root shadow element but it always times out. I did try waitForElement and delay but still the same. So, is there a any possible way to capture elements of a shadow DOM?

sindresorhus commented 2 years ago

This package just calls Puppeteer APIs underneath, so either you're using the incorrect selector or Puppeteer does not support shadow root selectors in their APIs.

https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pagewaitforselectorselector-options

schardev commented 2 years ago

Thanks for the quick response. So it turns out Puppeteer's selector interface does support pierce-ing through the shadow root, we just have to use a custom selector with a custom handler (which, for this task, is already provided by Puppeteer), so we can select a shadow dom element by using the following as a selector: pierce/<you-regular-selector>

Ref: https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#interface-selector

Closing this as the issue has been solved.