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

Playwright中使用.first()报错'Locator'不可调用,需点击首个按钮

解决Playwright点击第一个匹配元素的错误
  • 报错原因:你误将first属性当作方法调用了,Playwright中Locator的first是属性而非可调用方法,加括号会触发TypeError。
  • 正确写法(使用first属性):
    page.locator('button[aria-label="Toggle Comment Thread"]').first.click()
    
  • 其他可选实现方式:
    • 通过nth()方法指定索引(第一个元素索引为0):
      page.locator('button[aria-label="Toggle Comment Thread"]').nth(0).click()
      
    • 直接在CSS选择器中定位第一个匹配元素:
      page.locator('button[aria-label="Toggle Comment Thread"]:first-of-type').click()
      

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 04:55:54