how to open chrome in remote debug mode on mac
To open Chrome in remote debug mode on Mac, follow these steps:
Make sure you have Chrome installed on your Mac.
Open a Terminal window. You can do this by clicking on the "Spotlight Search" icon in the top-right corner of your screen, typing "Terminal", and then clicking on the Terminal app that appears in the search results.
In the Terminal window, enter the following command and press Enter:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
This command launches Chrome in remote debug mode and opens port 9222 for remote debugging connections.
Chrome should now open in a new window with a message indicating that it is listening for remote debugging connections.
You can now connect to Chrome in remote debug mode from another device or browser by entering chrome://inspect
in the address bar and clicking on the "Open dedicated DevTools for Node" link.
That's it! You have successfully opened Chrome in remote debug mode on your Mac.
how to use puppeteer to control it
To use Puppeteer to control Chrome in remote debug mode, follow these steps:
Install Puppeteer: Open a Terminal window and run the following command to install Puppeteer via npm (Node Package Manager):
npm install puppeteer
Create a new JavaScript file: Create a new file and name it something like remote-debug.js
.
Open the file in a code editor and add the following code to import Puppeteer and connect to the remote Chrome instance:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.connect({ browserURL: 'http://localhost:9222' });
// Your code to control Chrome goes here
})();
Add your code to control Chrome: Inside the async
function, you can add your Puppeteer code to control Chrome. For example, you can open a new page and navigate to a URL:
const page = await browser.newPage();
await page.goto('https://www.example.com');
Save the file and close the code editor.
Open a Terminal window and navigate to the directory where you saved the remote-debug.js
file.
In the Terminal, run the following command to execute the script and control Chrome:
node remote-debug.js
Puppeteer will connect to the remote Chrome instance and execute the code you added to control Chrome.
That's it! You have now used Puppeteer to control Chrome in remote debug mode on your Mac.
met an error: TypeError: Failed to fetch browser webSocket URL from http://localhost:9222/json/version: fetch failed
my cmd says: DevTools listening on ws://127.0.0.1:9222/devtools/browser/e881c02f-effa-4901-afc6-310ebd87799e
The error you encountered suggests that Puppeteer is unable to fetch the browser WebSocket URL from the provided URL. This can happen if the WebSocket URL is not accessible or if there are issues with the remote Chrome instance.
To resolve this issue, you can try the following steps:
Make sure you have started Chrome in remote debug mode with the --remote-debugging-port=9222
flag. Double-check that Chrome is running and listening on port 9222.
Verify that the remote Chrome instance is accessible by opening a new tab in your local Chrome browser and navigating to http://localhost:9222/json/version
. You should see a JSON response containing information about the remote Chrome instance. If you cannot access this URL, there may be an issue with the remote Chrome instance or your network configuration.
If the remote Chrome instance is accessible, check if there are any firewall settings or security software that may be blocking the connection. Temporarily disable any relevant settings or try running the script on a different network to see if the issue persists.
Confirm that you have the latest version of Puppeteer installed. You can update Puppeteer by running the following command in your Terminal:
npm update puppeteer
If none of the above steps resolve the issue, you can try using the WebSocket URL provided by your command line (ws://127.0.0.1:9222/devtools/browser/e881c02f-effa-4901-afc6-310ebd87799e
) instead of the browserURL
option in the puppeteer.connect()
function. Update your code to the following:
const browser = await puppeteer.connect({ browserWSEndpoint: 'ws://127.0.0.1:9222/devtools/browser/e881c02f-effa-4901-afc6-310ebd87799e' });
Replace the browserWSEndpoint
value with the WebSocket URL provided by your command line.
Try these steps, and hopefully, you can resolve the error and successfully control Chrome using Puppeteer in remote debug mode.