Playwright 测试库 - 选择器的父元素

Playwright test library - parent element for selector

请问如何通过 Playwright E2E 库获取文本选择器的父元素。

是修改选择器(它是由 >> //:parent 之类的字符串)还是评估选择器然后调用 DOM 元素更好?

(选择器内容未知)

谢谢。

您可以调用.$从元素开始搜索:

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

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

使用新的 Locator 您可以:

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

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

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