I have two Node.js scripts for puppeteer automation.
1) launcher.js
This Puppeteer script launches a chrome browser and disconnects the chrome so that it can be connected by using WSEndpoint.
const puppeteer = require('puppeteer');
module.exports = async () => {
try {
const options = {
headless: false,
devtools: false,
ignoreHTTPSErrors: true,
args: [
`--no-sandbox`,
`--disable-setuid-sandbox`,
`--ignore-certificate-errors`
]
};
const browser = await puppeteer.launch(options);
let pagesCount = await browser.pages();
const browserWSEndpoint = await browser.wsEndpoint();
// console WSEndPoint say : "ws://127.0.0.1:42207/devtools/browser/dbb2525b-ce44-43c2-a335-ff15d0306f36"
console.log("browserWSEndpoint----- :> ", browserWSEndpoint);
await browser.disconnect();
return browserWSEndpoint;
} catch (err) {
console.error(err);
process.exit(1);
return false;
}
};
2) connector.js
Launches headless chrome and tries to connect chrome by WSEndPoint by various hostnames. If I ran this script with run command as node connector.js localhost
, it tries to connect WSEndpint with localhost as the hostname.
const puppeteer = require('puppeteer');
const launcher = require('./launcher');
(async (host) => {
try {
let WSEndPoint = await launcher();
WSEndPoint = WSEndPoint.replace('127.0.0.1', host);
console.log("WSENDPOINT :", WSEndPoint);
const browser = await puppeteer.connect({
browserWSEndpoint: WSEndPoint,
ignoreHTTPSErrors: true
});
let pagesCount = await browser.pages();
console.log("Pages available :> ", pagesCount.length);
// const browserWSEndpoint = await browser.wsEndpoint();
await browser.disconnect();
process.exit(1);
return true;
} catch (err) {
console.error(err);
process.exit(1);
return false;
}
})(process.argv[2]);
But I can't connect the WSEndpint of the chrome using my local IP address ( say 192.168.1.36). Why?
The error message is
{ Error: connect ECONNREFUSED 192.168.1.33:36693
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.1.33',
port: 36693 }