Gab*_*sta 13 automated-tests node.js e2e-testing playwright
我正在尝试使用 Playwright 迭代动态元素列表,我已经尝试了一些方法,但没有一个有效:
await this.page.locator('li').click();
const elements = await this.page.locator('ul > li');
await elements.click()
Run Code Online (Sandbox Code Playgroud)
await this.page.$$('ul > li').click();
Run Code Online (Sandbox Code Playgroud)
await this.page.click('ul > li');
Run Code Online (Sandbox Code Playgroud)
const divCounts = await elements.evaluateAll(async (divs) => await divs.click());
Run Code Online (Sandbox Code Playgroud)
this.page.click('ul > li > i.red', { strict: false, clickCount: 1 },)
Run Code Online (Sandbox Code Playgroud)
const elements = await this.page.$$('ul > li > i.red')
elements.forEach(async value => {
console.log(value)
await this.page.click('ul > li > i.red', { strict: false, clickCount: 1 },)
await value.click();
})
Run Code Online (Sandbox Code Playgroud)
Mac*_*tyn 14
由于https://playwright.dev/docs/api/class-locator#locator-element-handles没有关于如何使用.elementHandles().
解决这个问题的另一种方法如下
const checkboxLocator = page.locator('tbody tr input[type="checkbox"]');
for (const el of await checkboxLocator.elementHandles()) {
await el.check();
}
Run Code Online (Sandbox Code Playgroud)
我设法用以下代码来做到这一点:
test('user can click multiple li', async ({ page }) => {
const items = page.locator('ul > li');
for (let i = 0; i < await items.count(); i++) {
await items.nth(i).click();
}
})
Run Code Online (Sandbox Code Playgroud)
小智 2
最近,剧作家 Slack 社区上也有人提出了类似的问题。这是复制粘贴的,并根据其中一位维护者的答案进行了最低限度的调整。
let listItems = this.page.locator('ul > li');
// In case the li elements don't appear all together, you have to wait before the loop below. What element to wait for depends on your situation.
await listItems.nth(9).waitFor();
for (let i = 0; i < listItems.count(); i++) {
await listItems.nth(i).click();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25881 次 |
| 最近记录: |