我试图通过CDP执行一些命令,但是无论我使用Selenium/Driver/Chrome的什么组合,结果总是一样的。
最后一次测试是:
这个项目是用C制作的,所以我是通过CURL手动发布到Selenium的。除了CDP之外,其他所有命令都能正常工作。
我已经检查过Selenium,Chrome驱动程序;它们都内置了CDP支持。
我试图发布的URL是:
- /session/id/goog/cdp/execute
- /session/id/{}/cdp/execute发布的数据格式是:"cmd“+ "params”(json对象)。
两个结果都是相同的: org.openqa.selenium.UnsupportedCommandException.
我还尝试在不同的模式下运行Selenium,独立运行,中心/节点,相同的结果。
有人能告诉我我做错了什么吗?或者我误解了这个用法?
发布于 2022-01-10 19:35:19
使用色驱动程序可执行文件
这对我有用(Windows +邮递员),但也应该与CURL /Mac.一起工作。
1下载铬驱动器:为您的铬版本。
2启动色度驱动器
start chromedriver.exe产出:
Starting ChromeDriver 97.0.4692.71 on port 9515...3向本地主机发送请求:9515/
POST localhost:9515/session
request json body:
{"capabilities":{"goog:chromeOptions": {}}}
status 200
response:
"value": {
"capabilities": {
...
},
"sessionId": "b8ac49ce2203739fa0d32dfe8d1a23b5"POST localhost:9515/session/b8ac49ce2203739fa0d32dfe8d1a23b5/url
request json body:
{"url": "https://example.com"}
status 200执行CDP命令(以screenshot):为例)
POST localhost:9515/session/b8ac49ce2203739fa0d32dfe8d1a23b5/goog/cdp/execute
request json body:
{"cmd":"Page.captureScreenshot", "params":{}}
status 200
response:
{
"value": {
"data": "iVBORw0KGgoAAAANSUhEUgA...."
}
}允许远程连接
默认情况下,chromedriver只允许本地连接。
为了允许一些远程IP:
start chromedriver.exe --allowed-ips="some-remote-ip"参考资料:https://sites.google.com/a/chromium.org/chromedriver/security-considerations
使用Selenium Grid运行CDP命令
最后,它开始对我起作用
注意:对于Selenium请求,内容类型标头应该有charset=utf-8 Content-Type:application/json;charset=utf-8。
先决条件
1根据https://www.selenium.dev/documentation/grid/getting_started/下载并运行selenium服务器
java -jar selenium-server-<version>.jar standalone --driver-configuration display-name='Chrome' stereotype='{"browserName":"chrome"}'2创建会话:
POST localhost:4444/wd/hub/session
request json body:
{
"desiredCapabilities": {
"browserName": "chrome",
"goog:chromeOptions": {
"args": [
],
"extensions": [
]
}
},
"capabilities": {
"firstMatch": [
{
"browserName": "chrome",
"goog:chromeOptions": {
"args": [
],
"extensions": [
]
}
}
]
}
}
status 200
response:
{
"status": 0,
"sessionId": "69ac1c82306f72c7aaf53cfbb28a30e7",
...
}
}3执行CDP命令(截图):
POST localhost:4444/wd/hub/session/69ac1c82306f72c7aaf53cfbb28a30e7/goog/cdp/execute
request json body:
{"cmd":"Page.captureScreenshot", "params":{}}
status 200
response:
{
"value": {
"data": "iVBORw0KGgoAAAANSUhEUgA...."
}
}https://stackoverflow.com/questions/70654898
复制相似问题