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.

[Feature] page.$ and page.$$ should pierce shadow DOM

See original GitHub issue

It seems like page.$ and page.$$ and their eval counterparts do not pierce the shadow DOM?

Should they maybe? It would not be equal to document.querySelector (or document.querySelectorAll) anymore then, but I’d say it is worth it?

E.g. when using page.$$ which is the only option when e.g. getting multiple elements in a table, to loop and do something on them, it would be nice it pierced the shadow DOM.

We also have another case in tests where we in an afterEach() try to close any open dialogs, just to make sure we have independence between test cases if one fails in the middle of an open dialog. In this case, in the afterEach() we do:

  // a-cool-button is in an open shadow DOM
  const dialog = await page.$('a-cool-dialog[show]');
  if (dialog) {
    await page.click(
      'a-cool-dialog[show] a-cool-button'
    );
  }

We would rather do:

  // a-cool-button is in an open shadow DOM
  const cancelButton= await page.$('a-cool-dialog[show] a-cool-button');
  if (cancelButton) {
    await cancelButton.click();
  }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
thernstigcommented, Apr 29, 2020

Nevermind, it does pierece the shadow DOM. (I’ll be honest here… it was second hand info that it did not work, I should have tested this myself, apologies).

However, the actual description for all these is not correct anymore. https://github.com/microsoft/playwright/blob/master/docs/api.md#pageselector etc. says:

The method runs document.querySelector within the page.

That cannot be true anymore since the functions pierce the shadow DOM, so it is not a direct mapping to that function which follows the CSS spec.

I assume then that the only difference between e.g. page.waitForSelector() and page.$() is that the latter do not have any wait at all? Is page.$ even needed anymore?

(personally I feel all the .$ functions are ambiguous due to no naming and should be deprecated in favor of better named functions where applicable).

0reactions
dgozmancommented, May 23, 2020

Hmm… Works for me with playwright v1.0.1:

const { chromium } = require('playwright');
(async () => {
  const browser = await chromium.launch({headless: false});
  const page = await browser.newPage();
  await page.goto('https://shop.polymer-project.org/');
  const sel = 'shop-tab';
  const btns = await page.$$(sel);
  await Promise.all([
    btns[0].click(),
    page.waitForNavigation(),
  ]);
  console.log(page.url());  // Prints https://shop.polymer-project.org/list/mens_outerwear
  await browser.close();
})();

What am I missing? Did you forget to await? Maybe the site is different?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shadow DOM v1 - Self-Contained Web Components
Shadow DOM composes different DOM trees together using the <slot> element. Slots are placeholders inside your component that users can fill with ...
Read more >
Using shadow DOM - Web Components | MDN
Shadow DOM allows hidden DOM trees to be attached to elements in the regular DOM tree — this shadow DOM tree starts with...
Read more >
Encapsulating Style and Structure with Shadow DOM
Currently, the only reliable way to style a shadow DOM node is by adding a <style> element to the shadow root's inner HTML....
Read more >
Styling: Styles Piercing Shadow DOM - Open Web Components
What this means is that inheritable styles, like color or font-family among others, continue to inherit in shadow DOM, will pierce the shadow...
Read more >
Is a Closed Shadow DOM within Content Script Secure?
Secure against what? What's your threat model? Extensions can definitely pierce the shadow dom. The untrusted page probably not, but the API ...
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