python - 手动创建新的 webdriver session

标签 python ubuntu selenium-webdriver webdriver python-requests

我正在尝试使用 Pythonrequests 模块对 WebDriver Protocol 进行一些尝试。

我启动了 chromedriver 二进制文件:

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

到目前为止一切顺利。

但问题是我在尝试创建 session 时遇到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'

最佳答案

好的,所以我研究了 Selenium 是如何做到的......:

selenium/webdriver/remote/remote_connection.py,类RemoteConnection,方法execute。我为参数提出了一个异常(exception),像这样:

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}}'`

关于python - 手动创建新的 webdriver session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45682073/

相关文章:

python - 使用 lxml/requests 从网页进行 TLE (Python3.6.4)

bash - 如何通过双击在 Linux 上运行脚本

bash - Ubuntu:仅允许为受限用户运行具有特定参数的特定命令

java - Selenium 中的代理身份验证弹出窗口

vba - Selenium 执行脚本

java - 使用 selenium pagefactory 时收到空指针异常

python - 重新分配 Pandas DataFrame 中的最大值

python - 从python中的类调用函数

opencv - 如何删除 openCV

python - “bool”对象不可调用