You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何定位并点击首个含3个selection的wrapper对应按钮

解决方案:点击首个包含恰好3个选项的容器内的按钮

以下是不同自动化测试工具下的实现代码:

Cypress

cy.get('[data-testid="wrapper"]')
  .filter((index, element) => {
    return Cypress.$(element).find('[data-testid^="selection"]').length === 3;
  })
  .first()
  .find('[data-testid="button"]')
  .click();

Playwright

简洁写法

await page.locator('[data-testid="wrapper"]')
  .filter({ has: page.locator('.selections').filter({ hasCount: 3 }) })
  .first()
  .locator('[data-testid="button"]')
  .click();

灵活判断写法

const targetWrapper = page.locator('[data-testid="wrapper"]').filter(async (wrapper) => {
  const selectionsCount = await wrapper.locator('[data-testid^="selection"]').count();
  return selectionsCount === 3;
});

await targetWrapper.first().locator('[data-testid="button"]').click();

Selenium (Java)

List<WebElement> wrappers = driver.findElements(By.cssSelector("[data-testid='wrapper']"));

for (WebElement wrapper : wrappers) {
    List<WebElement> selections = wrapper.findElements(By.cssSelector("[data-testid^='selection']"));
    if (selections.size() == 3) {
        wrapper.findElement(By.cssSelector("[data-testid='button']")).click();
        break;
    }
}

内容的提问来源于stack exchange,提问作者Ivan Haralampiev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 17:30:54