【问题标题】:CDP with Remote webdriver. 'WebDriver' object has no attribute 'execute_cdp_cmd' python带有远程 webdriver 的 CDP。 \'WebDriver\' 对象没有属性 \'execute_cdp_cmd\' python
【发布时间】:2022-10-13 03:34:36
【问题描述】:

我正在尝试使用 CDP 运行测试,

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

使用远程 webdriver(在 Selenoid 中)。但收到此错误:

AttributeError:“WebDriver”对象没有属性“execute_cdp_cmd”。 在本地环境中它工作正常。我尝试使用 Selenium 3.141.0 和 4.1.3。

我熟悉 PyCDP 文档 (https://py-cdp.readthedocs.io/en/latest/getting_started.html),但我不知道如何正确使用它。

为什么它不适用于远程 webdriver?有人有在 Selenium 4 中使用 python 执行 CDP 命令的示例吗?

我使用以下功能:

能力= {'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)

【问题讨论】:

  • 你是如何设置远程网络驱动程序的?你在远程使用什么?
  • 这是您的代码中的客户端问题。与 Selenoid 无关。
  • 嗨,大家好。这是设置代码: if request.config.getoption('--remote'): driver = webdriver.Remote(command_executor='selenoid<hidden>.dev:4444/wd/hub', desired_capabilities=capabilities, options=options) 远程测试完全正常,使用 CDP 的测试除外
  • @ewong 我使用以下功能:capabilities = { 'loggingPrefs': {'browser': 'ALL'}, 'goog:loggingPrefs': {'performance': 'ALL'}, "browserName": "chrome", " browserVersion": "99.0", "selenoid:options": { "enableVNC": True, "enableVideo": False } }
  • 请将其包含在您的原始帖子中,而不是在 cmets 中。

标签: python selenium selenium-chromedriver google-chrome-devtools selenoid


【解决方案1】:

看起来远程 Web 驱动程序不支持 CDP。

找到了这个很好的解决方法:

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', {})

来源及相关讨论:https://github.com/SeleniumHQ/selenium/issues/8672

【讨论】:

    猜你喜欢
    • 2021-06-23
    • 2022-01-08
    • 2022-11-24
    • 2012-10-06
    • 2022-06-27
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    相关资源
    最近更新 更多