Selenium常见异常:SessionNotCreatedException: Message: 无法创建会话:使用ChromeDriver Chrome Selenium时没有匹配的能力错误

5

首先,机器和软件规格: 我正在运行:

ChromeDriver version 75.0.3770.140
Selenium: version '3.141.0'
WSL (linux subsystem) of windows 10

我正在尝试通过Selenium运行Chrome浏览器。我发现了这些命令:这些, 可以通过Google Chrome使用Selenium。

我有一个测试目录,里面只有chromedriver二进制文件和脚本。目录的位置是:/home/kela/test_dir/

我运行了以下代码:

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')

这段代码的输出是:
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: No matching capabilities found

有人能解释一下为什么我需要使用capabilities,而其他人不需要就可以运行相同的脚本吗?我尝试过添加:

chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')

但我得到了相同的错误。所以,我不确定我需要添加什么能力(考虑到其他人没有问题)?

编辑1:回应DebanjanB下面的评论:

  1. Chromedriver在预期位置。我使用的是Windows 10。从这里,预期位置是C:\Program Files (x86)\Google\Chrome\Application\chrome.exe;这就是我的机器上它的位置(我从chrome属性表中复制并粘贴了此位置)。

  2. ChromeDriver对非根用户具有可执行权限。

  1. 我肯定已经安装了Google Chrome v75.0(我可以看到产品版本为75.0.3770.100)

  2. 我以非root用户身份运行脚本,因为我的bash命令行以$结尾而不是#(即kela:〜/test_dir $而不是kela:〜/test_dir#)

编辑2:根据DebanjanB下面的答案,我非常接近让它工作了,但还没有完全实现。

代码:

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/')

生成一个对话框,内容如下:

Google Chrome无法读取和写入其数据目录:/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只是“data:,”。如果我从脚本中删除“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开始偏离了这里具体讨论的问题,我在这里开了一个新问题here


3
编辑问题,将其限制在一个具体的问题上,并提供足够的细节以确定一个充分的答案。避免一次性提出多个不同的问题。请参阅如何提问页面以获取帮助澄清此问题。 - undetected Selenium
2
谢谢,我希望我已经让它更加简洁了。 - Slowat_Kela
谢谢,我同意有其他类似的问题,我在 OP 中标记了其中一个,你可以从我的编辑中看到,基于 DebanjanB 的答案,没有任何类似的问题/建议似乎适用于我的问题,我不确定下一步该怎么做,除了向社区询问如何解决我的具体问题(不是你建议的那个链接,因为它使用的是 geckodriver/firefox,而我的是 chromedriver/chrome,尽管我同意我最终找到的解决方案可能适用于不同的驱动程序)。 - Slowat_Kela
2个回答

3

This error message...

selenium.common.exceptions.SessionNotCreatedException: Message: session not created: No matching capabilities found

这意味着 ChromeDriver 无法启动/生成新的浏览器,即 Chrome 浏览器 会话。


binary_location

binary_location 设置/获取 Chrome (可执行)二进制文件的位置,并定义为:

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

根据您的代码尝试,options.binary_location='/home/kela/test_dir/chromedriver'是不正确的。

解决方案

如果 Chrome 安装在默认位置,则可以安全地删除此属性。如果 Chrome 安装在自定义位置,则需要使用 options.binary_location 属性指向 Chrome 安装位置。

您可以在 Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed 中找到详细讨论。

因此,您的代码块将是:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.binary_location=r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
driver = webdriver.Chrome(options=options, executable_path='/home/kela/test_dir/chromedriver.exe')
driver.get('http://google.com/')

此外,请确保以下内容:
  • 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.


1
谢谢,我尝试了所有这些解决方案,我已经在原帖中添加了编辑,但我不认为它们可以解决我的问题。 - Slowat_Kela
@Slowat_Kela,请查看答案更新并告诉我状态。 - undetected Selenium
谢谢,我已经编辑了我的问题以更新状态。我肯定更接近答案了,所以非常感谢。如果您认为我们现在已经偏离了原始问题的话题,我应该开一个新问题,请告诉我。在发布这里之前,我自己研究了当前的问题,但是在selenium和python的上下文中,我无法找到适用于这种情况的具体答案。 - Slowat_Kela
@Slowat_Kela 我也遇到了同样的问题。你找到解决方案了吗? - Balvant Ramani

0

你正在导入的是FireFox选项而不是Chrome选项,你必须确保导入了正确的选项来启动Selenium。

针对你的情况:

将这部分代码改为:

from selenium.webdriver.firefox.options import Options

转为:

from selenium.webdriver.chrome.options import Options

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接