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

selenium.common.exceptions.SessionNotCreatedException:消息:未创建会话:没有与ChromeDriver Chrome selenium匹配的功能错误,selenium,google-chrome,selenium-webdriver,webdriver,selenium-chromedriver,Selenium,Google Chrome,Selenium Webdriver,Webdriver,Selenium Chromedriver,首先,机器和包装规格: 我正在跑步: ChromeDriver version 75.0.3770.140 Selenium: version '3.141.0' WSL (linux subsystem) of windows 10 我正在尝试通过selenium运行chromebrowser。我发现:命令,通过谷歌浏览器使用硒 我有一个测试目录,其中只有chromedriver二进制文件和脚本。目录的位置是:/home/kela/test\u dir/ 我运行了代码: import sel

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

ChromeDriver version 75.0.3770.140
Selenium: version '3.141.0'
WSL (linux subsystem) of windows 10
我正在尝试通过selenium运行chromebrowser。我发现:命令,通过谷歌浏览器使用硒

我有一个测试目录,其中只有chromedriver二进制文件和脚本。目录的位置是:/home/kela/test\u 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的评论如下:

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

  • ChromeDriver对非root用户具有可执行权限

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

  • 我以非root用户的身份运行脚本,因为我的bash命令行以$and not#结尾(即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只是“数据:,”。如果我从脚本中删除行“driver.get(“”)”,则会发生同样的情况,因此我知道警告/问题与该行有关:

    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: Message: session not created: No matching capabilities found
    
    …意味着ChromeDriver无法启动/生成新的网络浏览器,即Chrome浏览器会话


    二进制位置 设置/获取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\u location='/home/kela/test\u dir/chromedriver'
    是不正确的


    解决方案 如果Chrome安装在默认位置,则可以安全地删除此属性。如果Chrome安装在自定义位置,则需要使用
    选项.binary\u location
    属性来指向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/')
    
    此外,请确保:

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

    • 以非root用户身份执行Selenium测试


    请将其局限于特定问题,并提供足够详细的信息,以确定适当的答案。避免同时问多个不同的问题。请参阅帮助澄清此问题的页面。谢谢,我希望它更简洁谢谢,我同意还有其他类似的问题,我在OP中标记了其中一个,根据DebanjanB的回答,您可以从我上面的编辑中看到,类似的问题/建议似乎都不适用于我的问题,我不确定下一步该怎么做,只是要求社区显示我的具体问题(虽然不是你建议的那个链接,因为它与geckodriver/firefox一起工作,而我的是chromedriver/chrome,尽管我同意我最终找到的解决方案可能适用于不同的驱动程序)。谢谢,我尝试了所有这些解决方案,我已经对OP添加了编辑,但我认为它们并没有解决我的问题。@Slowat_Kela检查答案更新并让我知道状态。谢谢,我编辑了上面的问题以更新状态。我肯定更接近了,所以我非常感谢。如果您认为我们现在已经偏离了or主题,请告诉我最初的问题,我应该提出一个新问题。我在这里发布之前自己也研究过我当前的问题,但在selenium和python的上下文中,我看不到一个具体的答案,在这种情况下它会起作用。@Slowat_Kela我也有同样的问题。你有什么解决方案吗?
    ---------ChromeDriver 75.0.3770.8 (2019-04-29)---------
    Supports Chrome version 75