【发布时间】:2017-08-14 19:53:21
【问题描述】:
我正在尝试使用Python 和requests 模块与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