我正在尝试运行一个简单的Puppeteer脚本,其中我打开Google Chrome,然后通过执行以下操作关闭它:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch(
{executablePath: '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'},
{headless: false},
{ignoreHTTPSErrors: true});
const page = await browser.newPage();
await page.goto('https://www.google.com');
await browser.close();
})().catch(error => { console.error("Something bad happend...", error); });;
然而,我收到的错误信息是:
Something bad happend... ErrorEvent {
target:
WebSocket {
_events:
[Object: null prototype] { open: [Function], error: [Function] },
_eventsCount: 2,
_maxListeners: undefined,
readyState: 3,
protocol: '',
_binaryType: 'nodebuffer',
_closeFrameReceived: false,
_closeFrameSent: false,
_closeMessage: '',
_closeTimer: null,
_closeCode: 1006,
_extensions: {},
_receiver: null,
_sender: null,
_socket: null,
_bufferedAmount: 0,
_isServer: false,
_redirects: 0,
url:
'ws://(my IP address)/devtools/browser/ae12c970-126d-463c-af41-1ab49be03575',
_req: null },
type: 'error',
message: 'connect ECONNREFUSED (my IP address)',
error:
{ Error: connect ECONNREFUSED (my IP address)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '(my IP address)',
port: (my port)} }
此外,我还尝试将我的代码运行为:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({executablePath: "/mnt/c/'Program Files (x86)'/Google/Chrome/Application/chrome.exe"});
const page = await browser.newPage();
await page.goto('https://www.google.com');
await browser.close();
})();
但我遇到了错误:
(node:2178) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process! spawn /mnt/c/'Program Files (x86)'/Google/Chrome/Application/chrome.exe ENOENT
我对JavaScript和Puppeteer还不是很熟悉,可能会错过一些显而易见的东西。这是自动填写在线表单的更大努力的一部分,因此似乎Puppeteer是前进的方式。但是,如果有人对如何做到这一点有更好的建议,那将不胜感激。有什么想法吗?
编辑1:我下载了Firefox并链接到它,而不是Chrome。当任务管理器开启时,在几秒钟后会出现一个弹窗。此外,在这个弹窗出现之前,Firefox只会显示为正在运行一次。您可以在以下链接的视频中查看该流程:https://vimeo.com/user119175210/review/436574888/590a38ef10。
127.0.0.1
。 - D. Pardal