相关疑难解决方法(0)

Puppeteer:在.evaluate()中传递变量

我正在尝试将变量传递给Puppeteer中page.evaluate()函数,但是当我使用以下非常简化的示例时,变量是未定义的.evalVar

我是Puppeteer的新手,找不到任何构建的例子,所以我需要帮助将该变量传递给page.evaluate()函数,以便我可以在里面使用它.

const puppeteer = require('puppeteer');

(async() => {

  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();

  const evalVar = 'WHUT??';

  try {

    await page.goto('https://www.google.com.au');
    await page.waitForSelector('#fbar');
    const links = await page.evaluate((evalVar) => {

      console.log('evalVar:', evalVar); // appears undefined

      const urls = [];
      hrefs = document.querySelectorAll('#fbar #fsl a');
      hrefs.forEach(function(el) {
        urls.push(el.href);
      });
      return urls;
    })
    console.log('links:', links);

  } catch (err) {

    console.log('ERR:', err.message);

  } finally {

    // browser.close();

  }

})();
Run Code Online (Sandbox Code Playgroud)

javascript evaluate web-scraping puppeteer

85
推荐指数
5
解决办法
4万
查看次数

如何在Puppeteers .evaluate()方法中传递函数?

每当我尝试传递一个函数时,就像这样:

var myFunc = function() { console.log("lol"); };

await page.evaluate(func => {
 func();
 return true;
}, myFunc);
Run Code Online (Sandbox Code Playgroud)

我得到:

(node:13108) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Evaluation failed: TypeError: func is not a function
at func (<anonymous>:9:9)
(node:13108) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Run Code Online (Sandbox Code Playgroud)

为什么?如何正确做?

谢谢!

€:让我澄清一下:我这样做是因为我想先找到一些DOM元素,然后在该函数内部使用它们,更像这样(简化):

var myFunc = function(element) { element.innerHTML = "baz" };

await page.evaluate(func => { …
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome node.js puppeteer

8
推荐指数
2
解决办法
4167
查看次数

为什么我不能在使用Puppeteer的exposeFunction()函数中访问'window'?

我有一个非常简单的Puppeteer脚本,用于exposeFunction()在无头Chrome中运行一些东西.

(async function(){

    var log = console.log.bind(console),
        puppeteer = require('puppeteer');


    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    var functionToInject = function(){
        return window.navigator.appName;
    }

    await page.exposeFunction('functionToInject', functionToInject);

    var data = await page.evaluate(async function(){
        console.log('woo I run inside a browser')
        return await functionToInject();
    });

    console.log(data);

    await browser.close();

})()
Run Code Online (Sandbox Code Playgroud)

这失败了:

ReferenceError: window is not defined
Run Code Online (Sandbox Code Playgroud)

其中指的是注入的功能.如何window在无头Chrome内部进行访问?

我知道我可以做evaluate(),但这不适用于我动态传递的函数:

(async function(){

    var log = console.log.bind(console),
        puppeteer = require('puppeteer');

    const browser = await puppeteer.launch(); …
Run Code Online (Sandbox Code Playgroud)

google-chrome node.js headless-browser puppeteer

7
推荐指数
2
解决办法
5036
查看次数

如何使用evaluateOnNewDocument和exposeFunction?

最近,我在一个新项目中使用了 Puppeteer。

我对 API 的一部分有一些我不明白的问题。文档对于这些API的介绍非常简单:

  1. page.exposeFunction
  2. page.evaluateOnNewDocument

可以给我一个详细的演示以便更好地理解吗?

javascript puppeteer

5
推荐指数
1
解决办法
1万
查看次数