无法读取未定义的“textContent”的属性
我有一段简单的代码,
describe('My First Puppeeteer Test', () => {
it('Should launch the browser', async function() {
const browser = await puppeteer.launch({ headless: false})
const page = await browser.newPage()
await page.goto('https://github.com/login')
await page.type('#login_field', testLogin)
await page.type('#password', testPassword)
await page.click('[name="commit"]')
await page.waitForNavigation()
let [element] = await page.$x('//h3[@class="text-normal"]')
let helloText = await page.evaluate(element => element.textContent, element);
console.log(helloText);
browser.close();
})
})
之前一切正常,但今天我收到一个错误+我的堆栈跟踪:
错误:评估失败:类型错误:无法读取未定义的属性(读取“textContent”) 在 puppeteer_evaluation_script:1:21 在 ExecutionContext._evaluateInternal (node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:221:19) 在 processTicksAndRejections (节点:内部/进程/task_queues:96:5) 在异步 ExecutionContext.evaluate (node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:110:16) 在异步上下文中。 (测试\example.tests.js:16:22)
我该如何解决这个问题?
亲切的问候
I have a simple piece of code
describe('My First Puppeeteer Test', () => {
it('Should launch the browser', async function() {
const browser = await puppeteer.launch({ headless: false})
const page = await browser.newPage()
await page.goto('https://github.com/login')
await page.type('#login_field', testLogin)
await page.type('#password', testPassword)
await page.click('[name="commit"]')
await page.waitForNavigation()
let [element] = await page.$x('//h3[@class="text-normal"]')
let helloText = await page.evaluate(element => element.textContent, element);
console.log(helloText);
browser.close();
})
})
Everything worked before but today I get an error + my stacktrace:
Error: Evaluation failed: TypeError: Cannot read properties of undefined (reading 'textContent')
at puppeteer_evaluation_script:1:21
at ExecutionContext._evaluateInternal (node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:221:19)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async ExecutionContext.evaluate (node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:110:16)
at async Context. (tests\example.tests.js:16:22)
How I can resolve this?
Kind regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然我没有因为登录而测试代码,并且我假设您的选择器是正确的,但主要问题几乎可以肯定会
产生竞争条件。 文档澄清:
作为一个侧面,最好执行
waitForXPath
而不是$x
,尽管这似乎不太可能是根问题。不要忘记await
所有承诺,例如browser.close()
。此外,如果您使用 Jest,一旦您开始工作,您可能需要将
浏览器
和页面
管理移至beforeEach
/< code>afterEach 或beforeAll
/afterAll
块。对于所有测试用例使用相同的浏览器实例会更快,并且可以在每个用例之前/之后打开和关闭页面。While I haven't tested the code due to the login and I assume your selectors are correct, the main problem is almost certainly that
creates a race condition. The docs clarify:
As a side point, it's probably better to do
waitForXPath
rather than$x
, although this seems less likely the root problem. Don't forget toawait
all promises such asbrowser.close()
.Additionally, if you're using Jest, once you get things working, you might want to move the
browser
andpage
management tobeforeEach
/afterEach
orbeforeAll
/afterAll
blocks. It's faster to use the same browser instance for all test cases, and pages can be opened and closed before/after each case.