selenium 元素,常见,异常,会话未创建异常:消息:会话未创建:没有与ChromeDriver Chrome Selenium匹配的功能错误

zaqlnxep  于 2023-04-27  发布在  Go
关注(0)|答案(2)|浏览(262)

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

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

我试图通过selenium运行chromebrowser。我发现:these命令,通过google chrome使用selenium。
我有一个test目录,其中只有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下面的评论:

  1. Chromedriver在预期的位置,我使用的是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)
1.我以非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只是'data:,'。如果我从脚本中删除'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

pkwftd7m

pkwftd7m1#

此错误消息...

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

...表示ChromeDriver无法启动/生成新的WebBrowser,即Chrome Browser会话。

二进制位置

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无法启动: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 Test
h9vpoimq

h9vpoimq2#

如果您导入的是Firefox选项而不是Chrome选项,则必须确保导入正确的选项才能启动selenium
你的案子
更改此:

from selenium.webdriver.firefox.options import Options

对此:

from selenium.webdriver.chrome.options import Options

相关问题