【问题标题】:selenium.common.exceptions.SessionNotCreatedException: Message: session not created: No matching capabilities error with ChromeDriver Chrome Selenium [duplicate]selenium.common.exceptions.SessionNotCreatedException:消息:未创建会话:ChromeDriver Chrome Selenium 没有匹配功能错误 [重复]
【发布时间】:2019-11-27 06:46:54
【问题描述】:

首先,机器和包装规格: 我在跑步:

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

我正在尝试通过 selenium 运行 chromebrowser。我发现:these 命令,通过谷歌浏览器使用硒。

我有一个测试目录,其中只有 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

当同一个脚本适用于其他没有能力的人时,谁能解释为什么我需要能力?我确实尝试添加:

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

但我遇到了同样的错误。所以我不确定我需要添加哪些功能(考虑到没有它的其他人也可以使用它?)

编辑 1:在下面解决 DebanjanB 的 cmets:

  1. Chrome 驱动程序位于预期位置。我正在使用 Windows 10。来自here,预期位置是 C:\Program Files (x86)\Google\Chrome\Application\chrome.exe;这就是它在我机器上的位置(我从 chrome 属性表中复制并粘贴了这个位置)。

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

  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 cannot read and write to it's data directory: /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')

例如,从here,我尝试添加:

options.add_argument('--profile-directory=Default')

但同样的警告会弹出。

编辑 3:

由于编辑 3 开始转向与此处具体解决的问题不同的问题,因此我提出了一个新问题 here

【问题讨论】:

  • edit the question 将其限制为具有足够详细信息的特定问题,以确定适当的答案。避免一次问多个不同的问题。请参阅How to Ask 页面以获得澄清此问题的帮助。
  • 谢谢,我希望它更简洁
  • 谢谢,我同意还有其他类似的问题,我在 OP 中标记了其中一个,您可以根据 DebanjanB 的回答从我上面的编辑中看到,似乎没有类似的问题/建议为我的问题工作,我不知道下一步该怎么做,只是要求社区显示我的具体问题(不是你建议的那个特定链接,因为它与 geckodriver/firefox 一起工作,而我的是 chromedriver/chrome,虽然我同意我最终找到的解决方案可能适用于不同的驱动程序)。

标签: selenium google-chrome selenium-webdriver webdriver selenium-chromedriver


【解决方案1】:

此错误消息...

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

...暗示 ChromeDriver 无法启动/生成新的 WebBrowserChrome 浏览器 会话。


二进制位置

binary_location set/get(s) 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 property 指向 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 对非 root 用户具有可执行权限。
  • 当您使用 ChromeDriver v75.0 时,请确保您拥有推荐的 Google Chrome v75.0 版本:

    ---------ChromeDriver 75.0.3770.8 (2019-04-29)---------
    Supports Chrome version 75
    
  • 非 root 用户身份执行 Selenium 测试

【讨论】:

  • 谢谢,我尝试了所有这些解决方案,我已向 OP 添加了编辑,但我认为它们无法解决我的问题。
  • @Slowat_Kela 查看答案更新并告诉我状态。
  • 谢谢,我已经编辑了上面的问题以更新状态。我肯定更接近,所以我很感激。如果您认为我们现在偏离了原始问题的主题,请告诉我,我应该提出一个新问题。在发布到这里之前,我确实自己研究了我当前的问题,但我在 selenium 和 python 的上下文中看不到具体的答案,这在这种情况下会起作用。
  • @Slowat_Kela 我有同样的问题。你有什么解决办法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-29
  • 2020-04-24
  • 1970-01-01
  • 1970-01-01
  • 2020-02-28
  • 2020-06-30
相关资源
最近更新 更多