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

Support transparently selecting elements under a shadow DOM #475

Closed
ankur22 opened this issue Jul 29, 2022 · 1 comment · Fixed by #901
Closed

Support transparently selecting elements under a shadow DOM #475

ankur22 opened this issue Jul 29, 2022 · 1 comment · Fixed by #901
Labels
enhancement New feature or request

Comments

@ankur22
Copy link
Collaborator

ankur22 commented Jul 29, 2022

Tested against: dbede12

When trying to create a script for an example to go on the documentation page, I found that the checkbox wasn't being found and it would eventually timeout. The script I ran using k6-browser was:

import { sleep } from 'k6';
import launcher from 'k6/x/browser';

export default function () {
  const browser = launcher.launch('chromium', {
    headless: false,
  });
  const context = browser.newContext();
  const page = context.newPage();
  page.goto("https://interactive-examples.mdn.mozilla.net/pages/tabbed/input-checkbox.html");
  const checkbox = page.locator('#horns');
  checkbox.check();

  sleep(5);
  browser.close();
}

I tested the same scenario in PW using the following and it was able to correctly select the checkbox:

// @ts-check
const { test, chromium } = require('@playwright/test');

test.describe('Editing', () => {
  test('Type login creds', async () => {
    const browser = await chromium.launch({ headless: false });
    const ctx = await browser.newContext();
    const page = await ctx.newPage();
    await page.goto("https://interactive-examples.mdn.mozilla.net/pages/tabbed/input-checkbox.html");
    const l = page.locator('#horns');
    l.check();

    await new Promise(resolve => setTimeout(resolve, 5000));

    await browser.close();
  });
});
@ankur22 ankur22 added the bug Something isn't working label Jul 29, 2022
@imiric
Copy link
Contributor

imiric commented Jul 29, 2022

This seems like a more general problem with selecting elements under a shadow DOM.

If you notice, input#horns is under a #shadow-root element. In DevTools you can't select it with $('#horns') either, but have to do something like $('shadow-output').shadowRoot.querySelector('#horns'). It's likely that Playwright supports this lookup transparently in some way, and we'd have to look into it. So I think this is more of a missing feature than a bug.

BTW, this workaround should work:

const el = page.evaluate(() => {
  return document.querySelector('shadow-output').shadowRoot.querySelector('input#horns');
});

... though it didn't quite work for me on this page and I don't want to spend more time on this right now. 😓

@imiric imiric changed the title Checkbox is not being selected on the MDN example page Support transparently selecting elements under a shadow DOM Jul 29, 2022
@imiric imiric added the enhancement New feature or request label Jul 29, 2022
@inancgumus inancgumus removed the bug Something isn't working label Feb 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants