Search code examples
e2e-testingplaywright

Playwright test library - parent element for selector


please, how to get a parent element for a text selector by the Playwright E2E library.

Is better to modify the selector (it is string by something like >> //:parent) or evaluate the selector and then call the DOM element?

(The selector content is unknown)

Thank you.


Solution

  • You can call .$ to start searching from the element:

    const elem = await page.$(anySelector)
    const parent = await elem.$('xpath=..')
    

    Doc: https://playwright.dev/docs/api/class-elementhandle#elementhandleselector