5

When using Chrome as Selenium webdriver as follow:

from selenium import webdriver
driver = webdriver.Chrome(executable_path='chromedriver.exe')
driver.close()

The first line to stdout is always something like this:

DevTools listening on ws://127.0.0.1:13007/devtools/browser/53aa377a-3789-4a8a-a565-dfd0f3622d38

How can I get this address in code? I don't see any obvious method or attribute (just judging from the name) of driver instance that might have this information.

Max Mikhaylov
  • 772
  • 13
  • 32

2 Answers2

4

I didn't find how to get it directly with the webdriver.
But here are two alternatives:

from selenium import webdriver

tmpChromeDir = 'c:\tmp\ChromeTmp'

options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=' + tmpChromeDir)

driver = webdriver.Chrome(chrome_options=options)

with open(tmpChromeDir + '/DevToolsActivePort') as fp:
    port = fp.readline().replace("\n", "")
    path = fp.readline().replace("\n", "")

print('---> ws://127.0.0.1:' + port + path)

or

from selenium import webdriver
import requests

options = webdriver.ChromeOptions()
options.add_argument('remote-debugging-port=9222')

driver = webdriver.Chrome(chrome_options=options)

result = requests.get('http://127.0.0.1:9222/json/version').json()

print(result['webSocketDebuggerUrl'])
Jerome
  • 1,153
  • 1
  • 17
  • 28
  • 1
    For the second option: If you do not know the remote debugging port (or you do not want/can to hard-code it in the source code), then you can obtain host and port from webdriver: `webdriver.capabilities['goog:chromeOptions']['debuggerAddress']` – eNca Jan 03 '22 at 09:28
0

The host: driver.command_executor._conn.host

The rest: If this is possible to attain, I'm pretty sure you will need to execute javascript using the webdriver. I cannot find any obvious ways to glean this, but I'm curious as to why this is useful to you during runtime?

Aphid
  • 343
  • 1
  • 9