首页 > 解决方案 > 自动与 Flash 内容交互

问题描述

我正在尝试自动从该站点抓取数据,该站点使用 Flash 来选择日期。为此,我尝试在 NodeJS 中使用 Puppeteer,但没有成功,浏览器不与 flash 按钮交互并返回错误消息:

(node:4264) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'waitForSelector' of undefined
at ~/scraper.js:27:19
at processTicksAndRejections (internal/process/task_queues.js:97:5)

(节点:4264)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。要在未处理的 Promise 拒绝时终止节点进程,请使用 CLI 标志--unhandled-rejections=strict(请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。(拒绝 id:1)(节点:4264)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。

下面是我的代码

const puppeteer = require('puppeteer-extra')
puppeteer.use(require('puppeteer-extra-plugin-flash')());

(async () => {
  const args = [
    '--enable-webgl',
    '--enable-accelerated-2d-canvas'
  ];
  const options = {
    args,
    headless: false,
    ignoreHTTPSErrors: false,
  };
  const browser = await puppeteer.launch(options)
  const page = await browser.newPage()

  const navigationPromise = page.waitForNavigation()

  await page.goto('https://www.peacebridge.com/index.php/historical-traffic-statistics/daily-volumes')

  await page.setViewport({ width: 1853, height: 981 })

  let frames = await page.frames()
  const frame_627 = frames.find(f => f.url() === 'https://www.peacebridge.com/traffic.php')
  // await frame_627.select('.interface > tbody > tr > td > .formfields:nth-child(1)', '2')

  await frame_627.waitForSelector('.interface > tbody > tr > td > .formfields:nth-child(1)')
  await frame_627.click('.interface > tbody > tr > td > .formfields:nth-child(1)')

  await frame_627.waitForSelector('body > .interface:nth-child(1) > tbody > tr > td')
  await frame_627.click('body > .interface:nth-child(1) > tbody > tr > td')

  await frame_627.select('.interface > tbody > tr > td > .formfields:nth-child(1)', '3')

  await frame_627.waitForSelector('.interface > tbody > tr > td > .formfields:nth-child(1)')
  await frame_627.click('.interface > tbody > tr > td > .formfields:nth-child(1)')

  await frame_627.select('.interface > tbody > tr > td > .formfields:nth-child(2)', '31')

  await frame_627.waitForSelector('.interface > tbody > tr > td > .formfields:nth-child(2)')
  await frame_627.click('.interface > tbody > tr > td > .formfields:nth-child(2)')

  await frame_627.select('.interface > tbody > tr > td > .formfields:nth-child(3)', '2010')

  await frame_627.waitForSelector('.interface > tbody > tr > td > .formfields:nth-child(3)')
  await frame_627.click('.interface > tbody > tr > td > .formfields:nth-child(3)')

  await frame_627.waitForSelector('.interface > tbody > tr > td > .buttons')
  await frame_627.click('.interface > tbody > tr > td > .buttons')

  await browser.close()
})()

标签: javascriptnode.jspuppeteer

解决方案


推荐阅读