Playwright:如何在元素树中搜索具有特定空标签的元素?

问题描述 投票:0回答:1

如何在 Playwright 测试中的元素树中搜索具有特定空标签的元素(使用他们的测试运行程序)?

我尝试过空文本过滤器(显然不是最好的主意)

// variant 1
page.locator(`[aria-label="label name"].has(span:text-is(""))`);
// variant 2
page.locator(`[aria-label="label name"].has(div[role="button"] span[text=""])`);

但出现错误

locator.textContent: Unexpected token "" while parsing selector "[aria-label="label name"]"

唯一适合我的解决方案是使用JS检查每个文本内容,例如:

const locator = page.locator('div[role="button"] >> span');
const elementsCount = await locator.count();
for(let i = 0; i < elementsCount; i++) {
  const spanTextContent = await locator.nth(i)?.textContent();
  return spanTextContent === '' ? locator.nth(i) : null;
}

另一个给出

Unexpected token "" while parsing selector
错误的例子:

    locator = page.locator(`body:has(div[class="L3eUgb"] >> a >> text="About")`);
    console.log(`locator.count(): ${await locator.count()}`);
javascript testing automated-tests end-to-end playwright
1个回答
0
投票

您似乎错误地混合了 CSS 选择器和自定义 Playwright 的伪类。

这应该适合你:

page.locator('div[role="button"] >> span >> text=""')

更新:

page.locator(`body:has(div[class="L3eUgb"]) >> a >> text="About"`)

© www.soinside.com 2019 - 2024. All rights reserved.