Search code examples
pythonnode.jsselenium-webdriverselenium-chromedriver

Trouble accessing Chrome's remote debugging port 9223 with Selenium WebDriver in headless mode


I'm trying to set up a Selenium WebDriver with Chrome in headless mode and remote debugging enabled. I'm using the following code to start Chrome and set up the WebDriver:

const { Builder, By, until, Capabilities } = require("selenium-webdriver");
const cheerio = require("cheerio");
const ps = require("ps-node");
const { exec, spawn } = require("child_process");
const fs = require("fs");
const net = require("net");
const fetch = require("node-fetch");

 async checkAndKillExistingChrome(port) {
    return new Promise((resolve, reject) => {
      ps.lookup(
        { command: "chrome", arguments: `--remote-debugging-port=${port}` },
        (err, resultList) => {
          if (err) {
            return reject(err);
          }

          const chromeProcess = resultList[0];
          if (chromeProcess) {
            console.log(
              `Killing existing Chrome process with PID: ${chromeProcess.pid}`
            );
            ps.kill(chromeProcess.pid, (err) => {
              if (err) {
                return reject(err);
              }
              console.log("Existing Chrome process terminated.");
              resolve();
            });
          } else {
            resolve();
          }
        }
      );
    });
  }

  async startChrome() {
    return new Promise(async (resolve, reject) => {
      const port = 9223;
      await this.checkAndKillExistingChrome(port);

      console.log(
        "Starting Chrome with remote debugging enabled and in headless mode..."
      );
      exec(
        `"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" --remote-debugging-port=${port} --user-data-dir=C:\\selenium\\ChromeProfile2 --headless --disable-gpu --no-sandbox --enable-logging --v=1 --log-file=C:\\path\\to\\chrome.log`,
        (error, stdout, stderr) => {
          if (error) {
            console.error(`Error starting Chrome: ${error}`);
            reject(error);
            return;
          }
          console.log(`Chrome stdout: ${stdout}`);
          console.error(`Chrome stderr: ${stderr}`);
        }
      );

      setTimeout(async () => {
        try {
          const response = await fetch(`http://127.0.0.1:${port}/json`);
          if (response.ok) {
            const jsonResponse = await response.json();
            if (jsonResponse.length > 0) {
              console.log(`Chrome is running and listening on port ${port}.`);
              resolve();
            } else {
              console.error(
                `Chrome is running on port ${port} but no pages are available.`
              );
              reject(
                new Error(
                  `Chrome is running on port ${port} but no pages are available.`
                )
              );
            }
          } else {
            console.error(`Chrome is not listening on port ${port}.`);
            reject(new Error(`Chrome is not listening on port ${port}.`));
          }
        } catch (err) {
          console.error(
            "Failed to connect to Chrome remote debugging port:",
            err
          );
          reject(err);
        }
      }, 15000); 
    });
  }

  async setupDriver() {
    try {
      console.log("Setting up the driver...");
      const chromeCapabilities = Capabilities.chrome();
      chromeCapabilities.set("goog:chromeOptions", {
        debuggerAddress: "127.0.0.1:9223", // Use 127.0.0.1 explicitly
      });

      this.driver = await new Builder()
        .withCapabilities(chromeCapabilities)
        .build();

      console.log("Driver setup complete.");
    } catch (err) {
      console.error("Error setting up driver:", err);
    }
  }

Error:-

Error setting up driver: SessionNotCreatedError: session not created: cannot connect to chrome at 127.0.0.1:9223
from unknown error: unable to discover open pages

The Chrome instance seems to be running fine and listening on port 9223, as confirmed by netstat. However, when I try to fetch the debugging information using curl http://127.0.0.1:9223/json, I get an empty response.

I've tried increasing the timeout, checking the Chrome logs, and verifying the command used to start Chrome, but I'm still unable to access the debugging information. Are there any other steps I can take to troubleshoot this issue?

Any help or guidance would be greatly appreciated. Thank you!

I tried starting Chrome with remote debugging enabled and in headless mode using a specific port (9223) and then accessing the debugging information using curl http://127.0.0.1:9223/json.

I expected to receive a JSON response containing the debugging information (e.g., list of debugging targets). However, I received an empty response instead.

I also checked the Chrome logs and verified that Chrome was running and listening on port 9223 using netstat.

What other steps can I take to troubleshoot this issue and successfully access the debugging information?


Solution

  • You need to open at least one page on the Chrome browser launched like

    "C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 --user-data-dir=C:\selenium\ChromeProfile --headless --disable-gpu https://www.example.com/