【问题标题】:Create new webdriver session manually手动创建新的 webdriver 会话
【发布时间】:2017-08-14 19:53:21
【问题描述】:

我正在尝试使用Pythonrequests 模块与WebDriver Protocol 一起玩。

我开始chromedriver二进制:

$ chromedriver_2.31 
Starting ChromeDriver 2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8) on port 9515
Only local connections are allowed.

到目前为止一切顺利。

但问题是我在尝试创建会话时收到session not created 异常:

import requests

r = requests.post("http://127.0.0.1:9515/session", {})

print("Status: " + str(r.status_code))
print("Body: " + str(r.content))

执行输出:

Status: 200
Body: b'{"sessionId":"286421fcd381ee0471418ebce7f3e125","status":33,"value":{"message":"session not created exception: Missing or invalid capabilities\\n  (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Linux 4.4.0-91-generic x86_64)"}}'

我搜索了WebDriver Protocol docs,但找不到有关哪些功能是强制性的或类似的信息。

所以,我尝试了一些随机功能:

import requests

data = {
        "browserName": "chrome",
        "version": "",
        "platform": "LINUX",
        "javascriptEnabled": "true",
        "acceptInsecureCerts": "false",
        "cssSelectorsEnabled": "true"
        }

r = requests.post("http://127.0.0.1:9515/session", data)

print("Status: " + str(r.status_code))
print("Body: " + str(r.content))

但又失败了:

Status: 400
Body: b'missing command parameters'

您有什么想法是什么问题以及如何解决它?

更新

也试过了:

import requests


data = """
        {
            desiredCapabilities: {  
            "browserName": "chrome",
            "version": "",
            "platform": "ANY"
            }
        }
    """

headers = {'Content-type': 'application/json'}

r = requests.post("http://127.0.0.1:9515/session", json=data, headers=headers)

print("Status: " + str(r.status_code))
print("Body: " + str(r.content))

又报错了:

Status: 400
Body: b'missing command parameters'

【问题讨论】:

  • 这是一个很好的问题.. 我正在修改它。看看这个:sites.google.com/a/chromium.org/chromedriver/capabilities
  • @JugurthaHadjar 您提到的链接是关于特定语言绑定到 WebDriver 协议的。我正在尝试将其用作原始 http 请求
  • 这不重要。我也在使用 requests 库(顺便说一下,驱动程序需要一个 json 对象,所以我想你会使用:r = requests.post('http://localhost:9515/session', json=data) 驱动程序有一个特定的 API,所以无论你在 Javascript 还是 Python 中找到资源,它们都必须尊重它。
  • 我搜索了异常消息,发现:github.com/bayandin/chromedriver/… 如您所见,这个词是desiredCapabilities。我查看 Selenium,发现:github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities
  • @JugurthaHadjar 请查看更新。这是你的意思吗?

标签: python ubuntu selenium-webdriver webdriver python-requests


【解决方案1】:

您需要发布包含功能和所需功能的 json 数据。 alwaysMatch 可以包含以下任何一个或多个:

  • 接受不安全证书
  • 浏览器名称
  • 浏览器版本
  • 平台名称
  • 页面加载策略
  • 代理
  • setWindowRect
  • 超时
  • unhandledPromptBehavior


    params = {
        'capabilities': {
            'firstMatch': [{}], 
            'alwaysMatch': {
                ...
            }
        },
        'desiredCapabilities': {
            'browserName': 'chrome',
            'version': '60',
            'platform': 'MAC'
        }
    }

r = requests.post('http://127.0.0.1:9515/session', json=params)

参考:

【讨论】:

    【解决方案2】:

    chromedriver(至少从 2.41 开始)根本不支持 WebDriver 协议。

    chromedriver所需的缺失能力是capabilities.alwaysMatch.goog:chromeOptions.w3c: true

    更新:chromedriver 76 有更好的 webdriver 支持(正常的 webdriver 客户端通常开始工作),但仍然没有完全实现协议。

    【讨论】:

      【解决方案3】:

      好的,所以我看看 Selenium 是如何做到的......:

      selenium/webdriver/remote/remote_connection.py,类RemoteConnection,方法execute。我提出了一个参数异常,如下所示:

      raise Exception(params)
      

      结果如下:

      {'capabilities': {'alwaysMatch': {'browserName': 'chrome',
         'chromeOptions': {'args': [], 'extensions': []},
         'platform': 'ANY',
         'version': ''},
        'firstMatch': []},
       'desiredCapabilities': {'browserName': 'chrome',
        'chromeOptions': {'args': [], 'extensions': []},
        'platform': 'ANY',
        'version': ''}}
      

      所以,用我们新发现的字典data 做同样的事情:

      r = requests.post('http://127.0.0.1:9515/session', json=data)
      # A new chromium window is created
      r.content
      `b'{"sessionId":"94cf6af9577d323eb51f6340b1fd2d07","status":0,"value":{"acceptSslCerts":true,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"browserName":"chrome","chrome":{"chromedriverVersion":"2.30.477729 (e5aa99d9d101379b1542958a71df3f50913f1ea2)","userDataDir":"/tmp/.org.chromium.Chromium.uqwViV"},"cssSelectorsEnabled":true,"databaseEnabled":false,"handlesAlerts":true,"hasTouchScreen":false,"javascriptEnabled":true,"locationContextEnabled":true,"mobileEmulationEnabled":false,"nativeEvents":true,"networkConnectionEnabled":false,"pageLoadStrategy":"normal","platform":"Linux","rotatable":false,"takesHeapSnapshot":true,"takesScreenshot":true,"unexpectedAlertBehaviour":"","version":"60.0.3112.78","webStorageEnabled":true}}'`
      

      【讨论】:

        猜你喜欢
        • 2017-06-03
        • 1970-01-01
        • 1970-01-01
        • 2016-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-07
        • 1970-01-01
        相关资源
        最近更新 更多