简体   繁体   中英

Chrome DevTools getting websocket address

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.

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'])

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM