3

我正在使用无头 Chrome 包Puppeteer运行测试:

const puppeteer = require('puppeteer')

;(async() => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.goto('https://google.com', {waitUntil: 'networkidle'})
  // Type our query into the search bar
  await page.type('puppeteer')

  await page.click('input[type="submit"]')

  // Wait for the results to show up
  await page.waitForSelector('h3 a')

  // Extract the results from the page
  const links = await page.evaluate(() => {
    const anchors = Array.from(document.querySelectorAll('h3 a'))
    return anchors.map(anchor => anchor.textContent)
  })
  console.log(links.join('\n'))
  browser.close()
})()

我将脚本运行为:node --harmony test/e2e/puppeteer/index.js(v6.9.1)

但我得到这个错误:

;(async() => {
       ^
SyntaxError: Unexpected token (

可能是什么问题呢?

注意:我使用的是 Vue CLI 的官方 Webpack 模板:

4

2 回答 2

6

我发现:节点 LTS(又名节点 6)现在不支持异步/等待机制。看 : 在此处输入图像描述

有关详细信息,请参见此处:https ://www.infoq.com/news/2017/02/node-76-async-await

于 2017-09-06T06:40:40.387 回答
1

lint 后,我​​在笔记本电脑上尝试了您的代码,它运行良好: 在此处输入图像描述

也许你有环境问题。

您是否考虑过删除行首的分号?它看起来不像正确的编程。或者可能是 webpack 问题。

于 2017-09-06T03:47:10.800 回答