简体   繁体   English

我怎么能传递这个变量来从我的模块中使用它?

[英]How I can pass this variable to use it from my module?

I've set a basic puppeteer code to automate chrome. 我已经设置了一个基本的木偶操作码来自动化chrome。 And I'm trying to pass the page variable as parameter to my module method using mymodule.foo(page) but it fails throwing the error you can see below: 我正在尝试使用mymodule.foo(page)page变量作为参数传递给我的模块方法,但它无法抛出您在下面看到的错误:

The goal is to be able to use puppeteer objects like page from other modules. 目标是能够使用木偶操纵者对象,如来自其他模块的page

E:\scripts\node\myproject\mymodule.js:3
    await page.evaluateOnNewDocument(() => {
          ^^^^

SyntaxError: Unexpected identifier
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:607:28)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (E:\scripts\node\myproject\app.js:3:18)
PS E:\scripts\node\myproject>

This is the file app.js : 这是app.js文件:

// app.js
const puppeteer = require('puppeteer');

const mymodule = require('./mymodule');

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

  await mymodule.foo(page);

  await browser.close();
})();

This is the file mymodule.js : 这是mymodule.js文件:

UPDATE: I've updated this file to add async to the foo function. 更新:我已更新此文件以向foo函数添加async But then If I try to use await page.goto('http://www.google.com'); 但是如果我尝试使用await page.goto('http://www.google.com'); with await I get the same error. 等待我得到同样的错误。 So I tried to add another async doing page.evaluateOnNewDocument(async () Then it runs, open the browser, but it doesn't navigate to the page, neither wait. 所以我尝试添加另一个async doing page.evaluateOnNewDocument(async ()然后它运行,打开浏览器,但它没有导航到页面,也没有等待。

// mymodule.js
module.exports = {
  foo: async function (page) {
    await page.evaluateOnNewDocument(async () => {
      await page.goto('http://www.google.com');
      await page.waitFor(10000);  // That's Evil, I know
    });
  }
};

UPDATE 2: This is the actual code (suggested by @Hongarc) I'm using, that doesn't throw errors, but doesn't work yet. 更新2:这是我正在使用的实际代码(由@Hongarc建议),它不会抛出错误,但是还没有工作。

module.exports = {
  foo: function (page) {
    return page.evaluateOnNewDocument(async () => {
      await page.goto('http://www.google.com');
      await page.waitFor(10000);  // That's Evil, I know
    });
  }
};

Another approach would be acceptable, whilst we can use ie. 另一种方法是可以接受的,而我们可以使用ie。 page.goto(<url>) inside of foo function of mymodule . page.goto(<url>)mymodulefoo函数里面。 If it work, it'll be ok. 如果它工作,它会没事的。

Let's face it, the following function will run a code on the browser context 让我们面对它,以下函数将在浏览器上下文中运行代码

page.evaluateOnNewDocument

The error is not on the module itself, but how you are using it. 错误不在模块本身,而在于您如何使用它。 page Object is not available on the browser context. page对象在浏览器上下文中不可用。 So calling it is not the solution. 所以称它不是解决方案。

If you want to use page. 如果你想使用页面。 in another module, you can safely do it this way, 在另一个模块中,你可以安全地这样做,

// mymodule.js
module.exports = {
  foo: async function (page) {
      await page.goto('http://www.google.com');
      await page.waitFor(10000);
  }
};

Also make sure you have node version 7.6 or higher. 还要确保您拥有7.6或更高版本的节点。

You need mark your function is async to use await . 你需要标记你的功能是async使用await Try this: 尝试这个:

// mymodule.js
module.exports = {
  foo: async function (page) {
    await page.evaluateOnNewDocument(async () => {
      await page.goto('http://www.google.com');
      await page.waitFor(10000);
    });
  }
};

This code will fix syntax of your code, but it wrong about logic. 此代码将修复代码的语法,但逻辑错误。

  • You don't need await in module.exports.foo of mymodule.js . 你不需要在mymodule.js module.exports.fooawait
  • async don't have competence use for function inside it. async没有能力用于其中的功能。

You can try: 你可以试试:

// mymodule.js
module.exports = {
  foo: function (page) {
    return page.evaluateOnNewDocument(async () => {
      await page.goto('http://www.google.com');
      await page.waitFor(10000);
    });
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM