selenium.common.exceptions.SessionNotCreatedException:消息:会话未创建:ChromeDriver Chrome Selenium 没有匹配功能错误 [英] selenium.common.exceptions.SessionNotCreatedException: Message: session not created: No matching capabilities error with ChromeDriver Chrome Selenium
问题描述
首先,机器和包装规格:我在跑步:
ChromeDriver 版本 75.0.3770.140硒:版本'3.141.0'Windows 10 的 WSL(linux 子系统)
我正在尝试通过 selenium 运行 chromebrowser.我发现:
我肯定安装了 Google Chrome v75.0(我可以看到产品版本 75.0.3770.100)
我以非 root 用户身份运行脚本,因为我的 bash 命令行以 $ 而不是 # 结尾(即 kela:~/test_dir$ 而不是 kela:~/test_dir#)
编辑 2:根据下面 DebanjanB 的回答,我非常接近让它工作,但还不够.
代码:
导入硒从硒导入网络驱动程序从 bs4 导入 BeautifulSoup从 selenium.webdriver.firefox.options 导入选项从 selenium.webdriver.firefox.firefox_binary 导入 FirefoxBinary从 selenium.webdriver.common.desired_capabilities 导入 DesiredCapabilities从 selenium.webdriver.chrome.options 导入选项选项=选项()options.binary_location='/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'驱动程序= webdriver.Chrome(选项=选项)driver.get('http://google.com/')
生成一个对话框,内容如下:谷歌浏览器无法读写其数据目录:/tmp/.com/google.Chrom.gyw63s
然后我再次检查了我的 Chrome 权限,我应该能够写入 Chrome:
另外,我可以看到/tmp/中有一堆 .com 目录:
.com.google.Chrome.4jnWme/.com.google.Chrome.FdNyKP/.com.google.Chrome.VAcWMQ/.com.google.Chrome.ZbkRx0/.com.google.Chrome.iRrceF/.com.google.Chrome.A2QHHB/.com.google.Chrome.G7Y51c/.com.google.Chrome.WD8BtK/.com.google.Chrome.cItmhA/.com.google.Chrome.pm28hN/
但是,由于这似乎更像是一个警告而不是错误,我单击确定"关闭对话框,并在浏览器中打开一个新选项卡;但 URL 只是数据:,".如果我从 'driver.get('http://google.com')'脚本,所以我知道警告/问题出在以下行:
driver = webdriver.Chrome(chrome_options = options,executable_path='/home/kela/test_dir/chromedriver')
例如,从这里,我尝试添加:
options.add_argument('--profile-directory=Default')
但同样的警告会弹出.
编辑 3:
由于编辑 3 开始转向与此处具体解决的问题不同的问题,因此我提出了一个新问题 这里.
这个错误信息...
selenium.common.exceptions.SessionNotCreatedException:消息:未创建会话:未找到匹配的功能
...暗示 ChromeDriver 无法启动/生成新的 WebBrowser 即 Chrome 浏览器 会话.
<小时>二进制位置
binary_location 设置/获取 Chrome(可执行)二进制文件的位置,定义为:
def binary_location(self, value):"""允许您设置铬二进制文件的位置:args:- 值:Chromium 二进制文件的路径"""self._binary_location = 值
因此,根据您的代码试验,options.binary_location='/home/kela/test_dir/chromedriver'
不正确.
解决方案
如果 Chrome 安装在默认位置,您可以安全地删除此属性.如果 Chrome 安装在自定义位置,您需要使用 options.binary_location
property 指向 Chrome安装.
您可以在 Selenium: WebDriverException:Chrome 无法启动:由于 google-chrome 不再运行而崩溃,因此 ChromeDriver 假定 Chrome 已崩溃
实际上,您的代码块将是:
从 selenium 导入 webdriver从 selenium.webdriver.chrome.options 导入选项选项=选项()options.binary_location=r'C:Program Files (x86)GoogleChromeApplicationchrome.exe'driver = webdriver.Chrome(options=options, executable_path='/home/kela/test_dir/chromedriver.exe')driver.get('http://google.com/')
此外,请确保以下几点:
- ChromeDriver 对非 root 用户具有可执行权限.
当您使用 ChromeDriver v75.0 时,请确保您拥有推荐的 Google Chrome v75.0 版本:
---------ChromeDriver 75.0.3770.8 (2019-04-29)------支持 Chrome 版本 75
以 非 root 用户身份执行 Selenium 测试.
First, machine and package specs: I am running:
ChromeDriver version 75.0.3770.140
Selenium: version '3.141.0'
WSL (linux subsystem) of windows 10
I am trying to run a chromebrowser through selenium. I found: these commands, to use selenium through google chrome.
I have a test directory, with only the chromedriver binary file, and the script, in it. The location of the directory is: /home/kela/test_dir/
I ran the code:
import selenium
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
options = Options()
options.binary_location='/home/kela/test_dir/chromedriver'
driver = webdriver.Chrome(chrome_options = options,executable_path='/home/kela/test_dir/chromedriver')
The output from this code is:
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: No matching capabilities found
Can anyone explain why I need capabilities when the same script works for others without capabilities? I did try adding:
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
but I got the same error. So I'm not sure what capabilities I need to add (considering it works for others without it?)
Edit 1: Addressing DebanjanB's comments below:
Chromedriver is in the expected location. I am using windows 10. From here, the expected location is C:Program Files (x86)GoogleChromeApplicationchrome.exe; and this is where it is on my machine (I copied and pasted this location from the chrome Properties table).
ChromeDriver is having executable permission for non-root users.
I definitely have Google Chrome v75.0 installed (I can see that the Product version 75.0.3770.100)
I am running the script as a non-root user, as my bash command line ends with a $ and not # (i.e kela:~/test_dir$ and not kela:~/test_dir#)
Edit 2: Based on DebanjanB's answer below, I am very close to having it working, but just not quite.
The code:
import selenium
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location='/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'
driver = webdriver.Chrome(options=options)
driver.get('http://google.com/')
Produces a dialog box that reads:
Google Chrome cannot read and write to it's data directory: /tmp/.com/google.Chrom.gyw63s
So then I double checked my Chrome permissions and I should be able to write to Chrome:
Also, I can see that /tmp/ has a bunch of .com dirs in it:
.com.google.Chrome.4jnWme/ .com.google.Chrome.FdNyKP/ .com.google.Chrome.VAcWMQ/ .com.google.Chrome.ZbkRx0/ .com.google.Chrome.iRrceF/
.com.google.Chrome.A2QHHB/ .com.google.Chrome.G7Y51c/ .com.google.Chrome.WD8BtK/ .com.google.Chrome.cItmhA/ .com.google.Chrome.pm28hN/
However, since that seemed to be more of a warning than an error, I clicked 'ok' to close the dialog box, and a new tab does open in the browser; but the URL is just 'data:,'. The same thing happens if I remove the line 'driver.get('http://google.com')' from the script, so I know the warning/issue is with the line:
driver = webdriver.Chrome(chrome_options = options,executable_path='/home/kela/test_dir/chromedriver')
For example, from here, I tried adding:
options.add_argument('--profile-directory=Default')
But the same warning pops up.
Edit 3:
As edit 3 was starting to veer into a different question than specifically being addressed here, I started a new question here.
This error message...
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: No matching capabilities found
...implies that the ChromeDriver was unable to initiate/spawn a new WebBrowser i.e. Chrome Browser session.
binary_location
binary_location set/get(s) the location of the Chrome (executable) binary and is defined as:
def binary_location(self, value):
"""
Allows you to set where the chromium binary lives
:Args:
- value: path to the Chromium binary
"""
self._binary_location = value
So as per your code trials, options.binary_location='/home/kela/test_dir/chromedriver'
is incorrect.
Solution
If Chrome is installed at the default location, you can safely remove this property. Incase Chrome is installed at a customized location you need to use the options.binary_location
property to point to the Chrome installation.
You can find a detailed discussion in Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed
Effectively, you code block will be:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location=r'C:Program Files (x86)GoogleChromeApplicationchrome.exe'
driver = webdriver.Chrome(options=options, executable_path='/home/kela/test_dir/chromedriver.exe')
driver.get('http://google.com/')
Additionally, ensure the following:
- ChromeDriver is having executable permission for non-root users.
As you are using ChromeDriver v75.0 ensure that you have the recommended version of the Google Chrome v75.0 as:
---------ChromeDriver 75.0.3770.8 (2019-04-29)--------- Supports Chrome version 75
Execute the Selenium Test as non-root user.
这篇关于selenium.common.exceptions.SessionNotCreatedException:消息:会话未创建:ChromeDriver Chrome Selenium 没有匹配功能错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!