8

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.

Tomas Randus
  • 2,127
  • 4
  • 29
  • 38

2 Answers2

8

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

Tomas Randus
  • 2,127
  • 4
  • 29
  • 38
7

Using the new Locator you can do:

  const elementParent = await page.locator(`${childSelector} >> xpath=..`)

https://playwright.dev/docs/api/class-locator

https://playwright.dev/docs/selectors#xpath-selectors

neil.morrison
  • 113
  • 1
  • 6