Coder Social home page Coder Social logo

Comments (3)

dgozman avatar dgozman commented on May 29, 2024 1

@RenderMichael Take a look at alternative methods for locating elements. Pretty much any other method except for XPath already works across open shadow roots.

We strongly recommend avoiding XPath in favor of user-visible locators like GetByRole(). Therefore, I don't think we'll be adding support for shadow dom in XPath locators.


If you really need XPath with manual shadow dom query, I'd recommend to register a custom selector engine that would query XPath in the shadow root. Something like this, but querying inside root.shadowRoot.

In this case the API would be:

page.Locator("xpath=//path/to/shadow/host").Locator("shadow-xpath=./path/to/target/from/the/shadow/root")

from playwright.

RenderMichael avatar RenderMichael commented on May 29, 2024 1

For posterity, this is the engine I wrote, which works for my case:

playwright.Selectors.RegisterAsync("xpath-shadow", new()
{
    ContentScript = true,
    Script =
    """
    {
        queryAll(root, selector) {
        if (selector.startsWith('/') && root.nodeType !== Node.DOCUMENT_NODE) {
            selector = '.' + selector;
        }

        const result = [];
        const document = root.ownerDocument || root;
        if (!document)
            return result;

        if (!root.shadowRoot) {
            return result; // if there's no shadow root, no error. just "not exists".
        }

        const firstElementInShadow = root.shadowRoot.querySelector("*");

        const selectorFromParent = '../' + selector;

        const it = document.evaluate(selectorFromParent, firstElementInShadow, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE);
        for (let node = it.iterateNext(); node; node = it.iterateNext()) {
            if (node.nodeType === Node.ELEMENT_NODE)
            result.push(node);
        }
        return result;
        }
    }
    """,
});

from playwright.

RenderMichael avatar RenderMichael commented on May 29, 2024

@dgozman Thanks for the swift response and the workaround tip!

from playwright.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.