【问题标题】:having trouble getting data from puppeteer .innertext js从 puppeteer .innertext js 获取数据时遇到问题
【发布时间】:2020-07-13 15:57:29
【问题描述】:

我无法获取元素中列出的文本。我以为我做对了,但我不断收到错误。这是我试图得到的元素。任何帮助都会很棒。无法链接该站点,因为您必须登录才能访问此页面。

<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)```

【问题讨论】:

  • 你可以尝试添加一些waitForSelector吗?
  • 由于您使用类查询,您需要添加一个点,如document.querySelector('.query-player_current')
  • 您忘记在 querySelector 中的选择器名称前添加 .,这就是它返回 null 的原因。
  • 嗯好吧。现在我不能控制台记录它?我尝试了 console.log(value) 但没有用。说值未定义。

标签: javascript puppeteer innertext


【解决方案1】:

你必须使用'.'作为前缀,如果您在查询中使用类名。 试试

 document.querySelector('.query-player_current')
 document.querySelector('.query-player_max')

Documentation

【讨论】:

    【解决方案2】:

    我得到了那个工作。现在我正在尝试做

            let whitlist = document.querySelector('.form-control').innerText;
    
            return {
                whitlist
            }
    
        });
    

    它不会工作。它

     Error: Evaluation failed: TypeError: Cannot read property 'innerText' of null
    

    元素在下面

    <textarea class="form-control" data-toggle="tooltip" data-container=".settings-form" data-html="true" data-placement="bottom" title="" id="form_base_whitelist" name="form[base][whitelist]" data-original-title="">
    Nasty CEO
    </textarea>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-17
      • 2017-06-12
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多