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

运行Lighthouse用户流报错:containerHandle.executionContext不是函数

解决pptr-testing-library与Puppeteer 21.x兼容问题导致的TypeError

问题根源

你遇到的TypeError: containerHandle.executionContext is not a function错误,是因为pptr-testing-library 0.7.0版本未适配Puppeteer 21.x的API变更。Puppeteer 21.x对ElementHandle的executionContext方法做了调整,而旧版pptr-testing-library仍在使用废弃的调用方式。

可行解决方案

方案1:升级pptr-testing-library到最新兼容版本

pptr-testing-library的后续版本已修复与Puppeteer 21+的兼容问题,执行以下命令升级:

npm install pptr-testing-library@latest

升级后重新运行代码,即可正常使用getByLabelText、getByText等查询方法。

方案2:降级Puppeteer到pptr-testing-library 0.7.0支持的版本

若不想升级pptr-testing-library,可将Puppeteer降级到20.x版本(pptr-testing-library 0.7.0明确支持的版本范围),执行命令:

npm install puppeteer@20.1.2

方案3:替换为Puppeteer原生查询方法(不依赖pptr-testing-library)

若不想调整依赖版本,可直接用Puppeteer原生API替代pptr-testing-library的查询方法,示例如下:

  • 替代getByLabelText:
// 原pptr-testing-library写法
// const input = await queries.getByLabelText(page, 'Username');

// Puppeteer原生写法
const input = await page.$('label:has-text("Username") + input');
// 或更精准的选择器
const input = await page.$('[aria-label="Username"]');
  • 替代getByText:
// 原pptr-testing-library写法
// const button = await queries.getByText(page, 'Submit');

// Puppeteer原生写法
const button = await page.$('button:has-text("Submit")');

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 10:17:37