Selenium 4.x trying to POST CDP: "UnsupportedCommandException"

Solution 1:

Using chromedriver executable

This worked for me (Windows + Postman), but should also work with CURL Linux/Mac.

1 Download chromedriver: https://chromedriver.chromium.org/downloads for your chrome version.

2 Start chromedriver

start chromedriver.exe

output:

Starting ChromeDriver 97.0.4692.71 on port 9515...

3 Send requests to localhost:9515/

  • 3.1 Create Session:
POST localhost:9515/session

request json body:
{"capabilities":{"goog:chromeOptions": {}}}

status 200

response:
 "value": {
        "capabilities": {
            ...
        },
        "sessionId": "b8ac49ce2203739fa0d32dfe8d1a23b5"
  • 3.2 Navigate some url (optional, just check request by sessionId works):
POST localhost:9515/session/b8ac49ce2203739fa0d32dfe8d1a23b5/url

request json body:
{"url": "https://example.com"}

status 200
  • 3.3 Execute CDP command (take screenshot):
POST localhost:9515/session/b8ac49ce2203739fa0d32dfe8d1a23b5/goog/cdp/execute

request json body:
{"cmd":"Page.captureScreenshot", "params":{}}

status 200

response:
{
    "value": {
        "data": "iVBORw0KGgoAAAANSUhEUgA...."
    }
}

Allow remote connections

By default chromedriver allows only local connections.

To allow some remote IPs:

start chromedriver.exe --allowed-ips="some-remote-ip"

Reference: https://sites.google.com/a/chromium.org/chromedriver/security-considerations

Run CDP commands with Selenium Grid

Eventually, it started to work for me with

  • ChromeDriver 97.0.4692.71
  • selenium-server-4.1.1
  • Chrome 97.0.4692.71 (Official Build) (64-bit)

Note: Content-Type header should have charset=utf-8 Content-Type:application/json;charset=utf-8 for Selenium Grid HTTP requests.

Prerequisites

1 Download and run selenium server according to https://www.selenium.dev/documentation/grid/getting_started/

java -jar selenium-server-<version>.jar standalone --driver-configuration display-name='Chrome' stereotype='{"browserName":"chrome"}'

2 Create Session:

POST localhost:4444/wd/hub/session

request json body:
{
  "desiredCapabilities": {
    "browserName": "chrome",
    "goog:chromeOptions": {
      "args": [
      ],
      "extensions": [
      ]
    }
  },
  "capabilities": {
    "firstMatch": [
      {
        "browserName": "chrome",
        "goog:chromeOptions": {
          "args": [
          ],
          "extensions": [
          ]
        }
      }
    ]
  }
}

status 200

response:
{
    "status": 0,
    "sessionId": "69ac1c82306f72c7aaf53cfbb28a30e7",
    ...
    }
}

3 Execute CDP command (take screenshot):

POST localhost:4444/wd/hub/session/69ac1c82306f72c7aaf53cfbb28a30e7/goog/cdp/execute

request json body:
{"cmd":"Page.captureScreenshot", "params":{}}

status 200

response:
{
    "value": {
        "data": "iVBORw0KGgoAAAANSUhEUgA...."
    }
}