Selenium: Enable execute_cdp for webdriver.Remote ?

Created on 4 Sep 2020  路  3Comments  路  Source: SeleniumHQ/selenium

馃殌 Feature Proposal

Can we enable execute_cdp for driver instance from webdriver.Remote()?
currently we are getting an Attribute Error for the following

Requiremnts

Python 3.6.9
selenium 3.141.0
from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox")
driver = webdriver.Remote( 
  command_executor='https://remote/webdriver', 
  desired_capabilities=chrome_options.to_capabilities() 
)
driver.execute_cdp_cmd("execute something")

AttributeError: 'WebDriver' object has no attribute 'execute_cdp_cmd'

Motivation

Already execute_cdp_cmd is availbe for selenium.webdriver.chrome.webdriver
https://www.selenium.dev/selenium/docs/api/py/webdriver_chrome/selenium.webdriver.chrome.webdriver.html#selenium.webdriver.chrome.webdriver.WebDriver.execute_cdp_cmd

Example

driver.execute_cdp_cmd("execute something")

C-py R-awaiting answer

Most helpful comment

A workaround would be using this function:

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

source: https://stackoverflow.com/a/47298910/8524395

All 3 comments

A workaround would be using this function:

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

source: https://stackoverflow.com/a/47298910/8524395

If you use pip install --pre selenium you should get the ability to execute CDP commands.
It's in the code base with this line: https://github.com/SeleniumHQ/selenium/blob/trunk/py/selenium/webdriver/remote/webdriver.py#L117

Closing this as the new mechanisms for using CDP are available to remote. We won't be adding this command as it was a workaround by the Chrome team

Was this page helpful?
0 / 5 - 0 ratings