Python Ubuntu:selenium.common.exceptions:未创建会话:此版本的ChromeDriver仅支持ChromeVersion79

Python Ubuntu:selenium.common.exceptions:未创建会话:此版本的ChromeDriver仅支持ChromeVersion79,python,selenium,google-chrome,ubuntu,selenium-chromedriver,Python,Selenium,Google Chrome,Ubuntu,Selenium Chromedriver,我有一个python脚本在AWS上的EC2实例(ubuntu)上运行。它使用硒。几周来,它一直工作正常,但今天突然,它停止了工作,出现了以下错误: selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 79 这是我在ubuntu上运行的python脚本: #inst

我有一个python脚本在AWS上的EC2实例(ubuntu)上运行。它使用硒。几周来,它一直工作正常,但今天突然,它停止了工作,出现了以下错误:

selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 79
这是我在ubuntu上运行的python脚本:

#install dependencies
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import ElementNotVisibleException
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#Set up chromedriver
options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--window-size=1420,1080')
options.add_argument('--headless')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')
options.add_argument("--disable-notifications")

driver = webdriver.Chrome(chrome_options=options)
奇怪的是chromedriver和Chrome浏览器似乎是兼容的

运行
chromedriver-v
后,我看到的版本是:

ChromeDriver 79.0.3945.79 (29f75ce3f42b007bd80361b0dfcfee3a13ff90b8-refs/branch-heads/3945@{#916})
运行chromium浏览器--version我得到:

Chromium 79.0.3945.79 Built on Ubuntu , running on Ubuntu 18.04
运行chromium browser-v时,我看到:

(chromium-browser:2901): Gtk-WARNING **: 17:28:14.613: cannot open display: 
我希望回答两个问题:

  • chromedriver和chrome怎么能工作几个星期,然后突然决定不合作?可能是chromedriver或chrome被更新而另一个没有被更新吗?除了更新从crontab运行脚本的时间之外,我没有更改任何内容

  • 当我的chromedriver和chrome浏览器的版本完全相同时,为什么会发生此错误?让chromedriver在ubuntu上使用chrome(无头)是一个非常漫长的过程,如果可能的话,我想“设置它并忘记它”。希望能更好地理解这个问题,这样我就可以避免它一次又一次地发生


  • 谢谢。

    默认情况下,
    webdriver.Chrome
    运行
    /usr/bin/google-Chrome
    (如果可用),而不是
    Chrome-browser
    (请参阅)。检查google chrome--version

    此错误消息

    selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 79
    
    …意味着ChromeDriver v79无法启动/生成新的浏览上下文,即浏览器版本不是v79.x的Chrome浏览器会话


    您的主要问题是所使用的二进制文件版本之间的不兼容,如下所示:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.binary_location='/path/to/chromium-browser.exe'
    driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe', options=options)
    driver.get('http://google.com/')
    
    • 您提到使用chromedriver=79.0.3945.79。尽管ChromeDriver v79.x的发布版本是和,但ChromeDriver=79.0.3945.79的发行说明明确提到以下内容:
    支持Chrome v79

    • 您正在使用chromium浏览器v79.0.3945.79浏览器
    • ChromeDriver安装在底层操作系统的默认位置时,支持
      google chrome

    1对于Linux系统,ChromeDriver希望
    /usr/bin/google chrome
    是实际chrome二进制文件的符号链接


    解决方案 有两种解决方案:

    • 您可以将安装在默认位置的
      google chrome
      升级到当前的chrome 79.0版本。(根据)
    • 或者您可以使用
      Chrome浏览器
      二进制位置覆盖默认的Chrome二进制位置,即
      /usr/bin/google Chrome
      ,如下所示:

      from selenium import webdriver
      from selenium.webdriver.chrome.options import Options
      
      options = Options()
      options.binary_location='/path/to/chromium-browser.exe'
      driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe', options=options)
      driver.get('http://google.com/')
      
    您可以在中找到详细的讨论

    • 通过IDE清理项目工作区,并仅使用所需的依赖项重建项目
    • 如果您的基本Web客户端版本太旧,请卸载它并安装最新的GA和Web客户端发布版本
    • 重新启动系统
    • 以非root用户身份执行
      @Test
    • 始终在
      tearDown(){}
      方法中调用
      driver.quit()
      ,以优雅地关闭和销毁Web驱动程序和Web客户端实例

    参考文献 有关详细讨论,请参见:


    谢谢你,马特。google chrome--版本显示:google chrome 78.0.3904.70我需要在ubuntu中更新吗?Debanjan,使用选项。binary_location='/path/to/chrome browser.exe'不工作,但使用选项。binary_location='/path/to/chrome browser'工作。@DiamondJoe12是,您需要更新Chrome或将ChromeDriver配置为使用Chrome。听起来后者对你有用。你能接受这个答案作为你问题的解决方案吗?马特-我会支持你的评论,但这里提供的另一个答案实际上能够解决这个问题。谢谢你的帮助!非常感谢。我的chromium-browser.exe在哪里?我的chrome浏览器在/usr/bin中,但当我这样做时:options.binary_location='/usr/bin/chromium browser.exe'终端读取消息:未知错误:在/usr/bin/chromium browser中没有chrome二进制文件。exe@DiamondJoe12如果您使用的是Linux Ubuntu,则需要去掉扩展名,即
    .exe
    部分