运行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
相关产品推荐
相关产品推荐

