selenium.common.exceptions.SessionNotCreatedException:消息:未创建会话:ChromeDriver Chrome Selenium 没有匹配功能错误

Slo*_*ela 5 selenium google-chrome webdriver selenium-chromedriver selenium-webdriver

首先,机器和包装规格:我正在运行:

ChromeDriver version 75.0.3770.140
Selenium: version '3.141.0'
WSL (linux subsystem) of windows 10
Run Code Online (Sandbox Code Playgroud)

我正在尝试通过 Selenium 运行 chrome 浏览器。我发现:这些命令,通过谷歌浏览器使用硒。

我有一个测试目录,其中仅包含 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')
Run Code Online (Sandbox Code Playgroud)

该代码的输出是:

selenium.common.exceptions.SessionNotCreatedException: Message: session not created: No matching capabilities found
Run Code Online (Sandbox Code Playgroud)

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

chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
Run Code Online (Sandbox Code Playgroud)

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

编辑 1:解决 DebanjanB 的以下评论:

  1. Chromedriver 位于预期位置。我使用的是 Windows 10。从这里开始,预期位置是 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/')
Run Code Online (Sandbox Code Playgroud)

生成一个对话框,内容如下: 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/
Run Code Online (Sandbox Code Playgroud)

但是,由于这似乎更像是警告而不是错误,因此我单击“确定”关闭对话框,并且浏览器中确实打开了一个新选项卡;但 URL 只是“data:,”。如果我从脚本中删除行 'driver.get(' http://google.com ')' ,也会发生同样的事情,所以我知道警告/问题与该行有关:

driver = webdriver.Chrome(chrome_options = options,executable_path='/home/kela/test_dir/chromedriver')
Run Code Online (Sandbox Code Playgroud)

例如,从这里,我尝试添加:

options.add_argument('--profile-directory=Default')
Run Code Online (Sandbox Code Playgroud)

但会弹出同样的警告。

编辑3:

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

Deb*_*anB 3

这个错误信息...

selenium.common.exceptions.SessionNotCreatedException: Message: session not created: No matching capabilities found
Run Code Online (Sandbox Code Playgroud)

...意味着ChromeDriver无法启动/生成新的WebBrowserChrome 浏览器会话。


二进制位置

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
Run Code Online (Sandbox Code Playgroud)

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


解决方案

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

您可以在 Selenium 中找到详细的讨论:WebDriverException:Chrome 无法启动:因 google-chrome 不再运行而崩溃,因此 ChromeDriver 假设 Chrome 已崩溃

实际上,您的代码块将是:

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/')
Run Code Online (Sandbox Code Playgroud)

此外,请确保以下几点:

  • ChromeDriver 对非 root 用户具有可执行权限。
  • 当您使用ChromeDriver v75.0时,请确保您拥有推荐的Google Chrome v75.0版本:

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


归档时间:

查看次数:

41933 次

最近记录:

3 年 前