gpt4 book ai didi

javascript - 如何使用 locator.evaluateAll() 在 Playwright 中获取基于 aria-label 的元素(按钮)数组?

转载 作者:行者123 更新时间:2023-12-05 04:26:20 25 4
gpt4 key购买 nike

我正在尝试从两个 aria-label 值之一获取一个 dom 元素数组。我正在尝试使用 locator.evaluateAll method 来做到这一点.

目前,这是我所能得到的;我试图获取我的 DOM 中的所有按钮,并从那些具有特定 aria-label 属性的按钮中创建一个数组:

const elements = this.page.locator('button');
const allSigButtons = await elements.evaluateAll(
(button) =>
`${button}[aria-label='Sign']` || `${button}[aria-label='Initial']`
);

当我将 allSigButtons 数组打印到控制台时,我得到:

[object HTMLButtonElement],[object HTMLButtonElement],[object HTMLButtonElement],[object HTMLButtonElement],[object HTMLButtonElement],[object HTMLButtonElement],[object HTMLButtonElement],[object HTMLButtonElement],[object HTMLButtonElement],[object HTMLButtonElement],[object HTMLButtonElement],[object HTMLButtonElement],[object HTMLButtonElement],[object HTMLButtonElement][aria-label='Sign']

显然我做错了什么!

最佳答案

我认为您正在寻找 comma-delimited CSS selectors (logical OR) :

const playwright = require("playwright");

const html = `
<body>
<button aria-label="Sign">foo</button>
<button aria-label="Signy">NO</button>
<button aria-label="Initial">bar</button>
<button aria-label="Initial">baz</button>
<button aria-label="Initia">NOT ME</button>
</body>
`;

let browser;
(async () => {
browser = await playwright.chromium.launch({headless: true});
const page = await browser.newPage();
await page.setContent(html);
const sel = "button[aria-label='Sign'], button[aria-label='Initial']";
const btns = await page.locator(sel);
console.log(await btns.allTextContents()); // => [ 'foo', 'bar', 'baz' ]
})()
.catch(err => console.error(err))
.finally(() => browser?.close())
;

关于javascript - 如何使用 locator.evaluateAll() 在 Playwright 中获取基于 aria-label 的元素(按钮)数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73086318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com