我正在尝试使用WebDriver Protocol
和Python
模块与requests
进行一点点。
我开始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'
答案 0 :(得分:2)
好的,所以我看了Selenium是如何做到的......:
selenium/webdriver/remote/remote_connection.py
,课程RemoteConnection
,方法execute
。我为params提出了一个例外,如下:
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}}'`
答案 1 :(得分:1)
您需要发布包含功能和所需功能的json数据。 alwaysMatch
可以包含以下任何一项或多项:
params = {
'capabilities': {
'firstMatch': [{}],
'alwaysMatch': {
...
}
},
'desiredCapabilities': {
'browserName': 'chrome',
'version': '60',
'platform': 'MAC'
}
}
r = requests.post('http://127.0.0.1:9515/session', json=params)
答案 2 :(得分:0)
chromedriver(至少从2.41开始)不支持WebDriver协议。
chromedriver所需的缺少功能是capabilities.alwaysMatch.goog:chromeOptions.w3c:true