Enumerating running Chrome browsers in Selenium

This is another article in our “Re-use selenium session” series. This article is specific to Chrome browser run on a local system using Selenium

Consider the below python code

from selenium import webdriver

driver = webdriver.Chrome()

This opens up a chrome browser on your machine. We know that Selenium uses chromewebdriver for communicating with the chrome. So let’s check the process

$ ps aux | grep chromedriver
tarun.lalwani     8805   0.0  0.1  2567048  17000 s003  S+   10:02PM   0:00.04 chromedriver --port=61208

We have an interesting argument passed to the chromerdriver which --port=61280. Let’s make a note of this. Now let’s get back to our python script and print the command executor url

print (driver.command_executor._url)
# prints 'http://127.0.0.1:61208'

As you can see this url has our previously noted down port. So by just looking at the chromedriver command line argument we can construct our command executor url. Now let’s use the command executor url on our terminal and check the /sessions endpoint

$ curl -sSL http://127.0.0.1:61208/sessions | jq
{
  "sessionId": "",
  "status": 0,
  "value": [
    {
      "capabilities": {
        "acceptSslCerts": true,
        "applicationCacheEnabled": false,
        "browserConnectionEnabled": false,
        "browserName": "chrome",
        "chrome": {
          "chromedriverVersion": "2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b)",
          "userDataDir": "/var/folders/4k/n292r2rj5_z3cb9ky0vh_szmk_m94b/T/.org.chromium.Chromium.h1HKri"
        },
        "cssSelectorsEnabled": true,
        "databaseEnabled": false,
        "handlesAlerts": true,
        "hasTouchScreen": false,
        "javascriptEnabled": true,
        "locationContextEnabled": true,
        "mobileEmulationEnabled": false,
        "nativeEvents": true,
        "networkConnectionEnabled": false,
        "pageLoadStrategy": "normal",
        "platform": "Mac OS X",
        "rotatable": false,
        "takesHeapSnapshot": true,
        "takesScreenshot": true,
        "unexpectedAlertBehaviour": "",
        "version": "59.0.3071.86",
        "webStorageEnabled": true
      },
      "id": "b46a551b76882f8fd5d3962b88624225"
    }
  ]
}

The interesting bit is "id": "b46a551b76882f8fd5d3962b88624225". That gives us our session id. And now we can use the approach disccused in our previous article Re-using Existing Browser Session in Python to reconstruct the driver.