如何在 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()}`);