首页 > 解决方案 > NodeJS Puppeteer:TypeError:无法读取 null 的属性“setAttribute”

问题描述

我收到错误TypeError: Cannot read property 'setAttribute' of null

这是元素:

<input type="text" name="fields[name]" class="form-control" placeholder="Full 
Name*" autocomplete="name" x-autocompletetype="name" 
spellcheck="false" autocapitalize="off" autocorrect="off">

这是我的代码(使用 puppeteer):

  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();
  await page.setUserAgent(userAgents[Math.floor(Math.random()*userAgents.length)]);

  await page.goto('website here');

  await page.evaluate(() => {

    document.querySelector('[placeholder="Full Name*"]').setAttribute('value', 'test');

  });

我也用过:

document.querySelector('[name="fields[name]"]').setAttribute('value', 'Test');

而且我也尝试过使用xpath,但都失败了。

重要的是要注意 querySelector 行在 chrome 控制台中运行良好,所以我不知道为什么它在这里不起作用......感谢任何帮助。

标签: javascriptnode.jstypeerrorpuppeteer

解决方案


推荐阅读