从 puppeteer .innertext js 获取数据时遇到问题
having trouble getting data from puppeteer .innertext js
我无法获取元素中列出的文本。我以为我做对了,但我不断出错。这是我试图获得的元素。任何帮助都会很棒。无法 link 该站点,因为您必须登录才能访问此页面。
<p><span class="query-player_current">1</span>
/
<span class="query-player_max">32</span></p>
(尝试获取“1”和“32”)^
这是我的代码。
const result = await page.evaluate (() => {
let playerCount = document.querySelector('query-player_current').innerText;
let playerCountMax = document.querySelector('query-player_max').innerText;
return {
playerCount,
playerCountMax
}
})
这是我遇到的错误
Error: Evaluation failed: TypeError: Cannot read property 'innerText' of null
at __puppeteer_evaluation_script__:2:73
at ExecutionContext._evaluateInternal (C:\Users\logan\Desktop\GamerBot\node_modules\puppeteer\lib\ExecutionContext.js:122:13)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async ExecutionContext.evaluate (C:\Users\logan\Desktop\GamerBot\node_modules\puppeteer\lib\ExecutionContext.js:48:12)
at async downloadLog (C:\Users\logan\Desktop\GamerBot\commands\test2.js:56:20)
at async Object.module.exports.run (C:\Users\logan\Desktop\GamerBot\commands\test2.js:15:3)
-- ASYNC --
at ExecutionContext.<anonymous> (C:\Users\logan\Desktop\GamerBot\node_modules\puppeteer\lib\helper.js:111:15)
at DOMWorld.evaluate (C:\Users\logan\Desktop\GamerBot\node_modules\puppeteer\lib\DOMWorld.js:112:20)
-- ASYNC --
at Frame.<anonymous> (C:\Users\logan\Desktop\GamerBot\node_modules\puppeteer\lib\helper.js:111:15)
at Page.evaluate (C:\Users\logan\Desktop\GamerBot\node_modules\puppeteer\lib\Page.js:860:43)
at Page.<anonymous> (C:\Users\logan\Desktop\GamerBot\node_modules\puppeteer\lib\helper.js:112:23)
at downloadLog (C:\Users\logan\Desktop\GamerBot\commands\test2.js:56:31)
at async Object.module.exports.run (C:\Users\logan\Desktop\GamerBot\commands\test2.js:15:3)```
您必须使用“.”作为前缀,如果您在查询中使用类名。
尝试
document.querySelector('.query-player_current')
document.querySelector('.query-player_max')
我无法获取元素中列出的文本。我以为我做对了,但我不断出错。这是我试图获得的元素。任何帮助都会很棒。无法 link 该站点,因为您必须登录才能访问此页面。
<p><span class="query-player_current">1</span>
/
<span class="query-player_max">32</span></p>
(尝试获取“1”和“32”)^
这是我的代码。
const result = await page.evaluate (() => {
let playerCount = document.querySelector('query-player_current').innerText;
let playerCountMax = document.querySelector('query-player_max').innerText;
return {
playerCount,
playerCountMax
}
})
这是我遇到的错误
Error: Evaluation failed: TypeError: Cannot read property 'innerText' of null
at __puppeteer_evaluation_script__:2:73
at ExecutionContext._evaluateInternal (C:\Users\logan\Desktop\GamerBot\node_modules\puppeteer\lib\ExecutionContext.js:122:13)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async ExecutionContext.evaluate (C:\Users\logan\Desktop\GamerBot\node_modules\puppeteer\lib\ExecutionContext.js:48:12)
at async downloadLog (C:\Users\logan\Desktop\GamerBot\commands\test2.js:56:20)
at async Object.module.exports.run (C:\Users\logan\Desktop\GamerBot\commands\test2.js:15:3)
-- ASYNC --
at ExecutionContext.<anonymous> (C:\Users\logan\Desktop\GamerBot\node_modules\puppeteer\lib\helper.js:111:15)
at DOMWorld.evaluate (C:\Users\logan\Desktop\GamerBot\node_modules\puppeteer\lib\DOMWorld.js:112:20)
-- ASYNC --
at Frame.<anonymous> (C:\Users\logan\Desktop\GamerBot\node_modules\puppeteer\lib\helper.js:111:15)
at Page.evaluate (C:\Users\logan\Desktop\GamerBot\node_modules\puppeteer\lib\Page.js:860:43)
at Page.<anonymous> (C:\Users\logan\Desktop\GamerBot\node_modules\puppeteer\lib\helper.js:112:23)
at downloadLog (C:\Users\logan\Desktop\GamerBot\commands\test2.js:56:31)
at async Object.module.exports.run (C:\Users\logan\Desktop\GamerBot\commands\test2.js:15:3)```
您必须使用“.”作为前缀,如果您在查询中使用类名。 尝试
document.querySelector('.query-player_current')
document.querySelector('.query-player_max')