Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CDP with Remote webdriver. 'WebDriver' object has no attribute 'execute_cdp_cmd' python

I'm trying to run tests with CDP,

webdriver.execute_cdp_cmd('Network.enable', {}) 

with Remote webdriver (in Selenoid). But getting this error:

AttributeError: 'WebDriver' object has no attribute 'execute_cdp_cmd'. In local environment it works fine. I've tried to use Selenium 3.141.0 and 4.1.3.

I'm familiar with PyCDP documentation (https://py-cdp.readthedocs.io/en/latest/getting_started.html) but I didn't figured out how to properly use it.

Why it does not work with Remote webdriver? Do someone have an example of executing CDP commands using python in Selenium 4?

I use following capabilities, :

capabilities = { 'loggingPrefs': {'browser': 'ALL'}, 'goog:loggingPrefs': {'performance': 'ALL'}, "browserName": "chrome", "browserVersion": "99.0", "selenoid:options": { "enableVNC": True, "enableVideo": False } }

if request.config.getoption('--remote'): driver = webdriver.Remote(command_executor='selenoid.dev:4444/wd/hub', desired_capabilities=capabilities, options=options)

like image 1000
Daniel Proskurin Avatar asked May 19 '26 16:05

Daniel Proskurin


1 Answers

Looks like CDP is not supported for remote webdrivers.

Found this sweet workaround the issue:

import json

def send(driver, cmd, params={}):
  resource = "/session/%s/chromium/send_command_and_get_result" % driver.session_id
  url = driver.command_executor._url + resource
  body = json.dumps({'cmd': cmd, 'params': params})
  response = driver.command_executor._request('POST', url, body)
  return response.get('value')

send(webdriver, 'Network.enable', {})

Relevant discussion: https://github.com/SeleniumHQ/selenium/issues/8672

like image 97
Hammad Avatar answered May 21 '26 05:05

Hammad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!