javascript - 自动与 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()
})()
解决方案
推荐阅读
- sql - 如何在 SQL 中不重复行?
- reactjs - 指向文件而不是目录时,Eslint 无法解析模块的路径
- java - 我可以使用自定义注解对 Java 类进行分类吗?
- html - 模态宽度内联 CSS 引导程序
- angular - Angular Http Interceptor 的“next.handle”方法是否使更新订阅符合 observable?
- swift - 为 ios App 设置 Firebase 云消息传递时遇到问题
- reactjs - nextjs 身份验证 (OAuth)
- reactjs - 基于 Reactjs 角色的仪表板链接
- arrays - 识别/合并 [] 范围数组中的相邻单元格,以限制 [] 范围数组中的#ranges
- javascript - discord.js 编辑斜杠命令交互消息