Ubuntu:selenium.common.exceptions:未创建会话:此版本的ChromeDriver仅支持Chrome版本79 [英] Ubuntu: selenium.common.exceptions: session not created: This version of ChromeDriver only supports Chrome version 79
问题描述
我有一个在AWS的EC2实例(ubuntu)上运行的python脚本.它使用硒.它运行了好几个星期,然后突然间,今天,它停止工作并出现以下错误:
I have a python script running on an EC2 instance (ubuntu) on AWS. It uses selenium. It was working perfectly for weeks, and then all of the sudden, today, it stopped working with the following error:
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 79
这是我在ubuntu上运行的python脚本:
Here is my python script, which I'm running on ubuntu:
#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浏览器似乎兼容.
What is odd is that chromedriver and chromium-browser appear to be compatible.
运行chromedriver -v
后,我看到的版本是:
Upon running chromedriver -v
I see the version is:
ChromeDriver 79.0.3945.79 (29f75ce3f42b007bd80361b0dfcfee3a13ff90b8-refs/branch-heads/3945@{#916})
然后,运行chromium-browser --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运行脚本的时间以外,我没有进行任何更改.
How could work for weeks, and then all of the sudden, chromedriver and chrome decide not to cooperate with each other? Could it be that either chromedriver or chrome was updated without the other being updated? I did not change anything, with the exception of updating the time from which the script was run from crontab.
当我的chromedriver和chrome浏览器版本完全相同时,为什么会发生此错误?要让chromedriver在ubuntu上使用chrome(无头),这是一个非常漫长的过程,如果可能,我想设置并忘记它".希望能更好地理解此问题,以免再次发生此问题.
Why is this error happening when my chromedriver and chrome browser are the exact same version? It was an extremely long process to get chromedriver to work with chrome (headless) on ubuntu and I'd like to "set it and forget it" if possible. Looking for away to better understand this problem so I can avoid it happening again and again.
谢谢.
推荐答案
此错误消息...
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 79
...表示 ChromeDriver v79 无法启动/产生新的 Browsing Context 即浏览器版本所在的 Chrome浏览器会话而不是v79.x.
...implies that the ChromeDriver v79 was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session where the browser version was other then v79.x.
您的主要问题是所使用的二进制文件版本之间的不兼容性:
Your main issue is the incompatibility between the version of the binaries you are using as follows:
- 您提到了有关使用 chromedriver = 79.0.3945.79 的信息.尽管 ChromeDriver v79.x 的发行版本是 ChromeDriver 79.0.3945.16(2019-10-30)和 ChromeDriver 79.0. 3945.36(2019-11-18),但是 chromedriver = 79.0.3945.79 的两个发行说明都明确提到了以下内容:
- You mentioned about using chromedriver=79.0.3945.79. Though the released versions of ChromeDriver v79.x are ChromeDriver 79.0.3945.16 (2019-10-30) and ChromeDriver 79.0.3945.36 (2019-11-18), however both the release notes of chromedriver=79.0.3945.79 clearly mentions the following :
支持 Chrome v79
- 您正在使用铬浏览器v79.0.3945.79 浏览器.
- ChromeDriver在相对于底层操作系统安装在默认位置时支持
google-chrome
: - You are using chromium-browser v79.0.3945.79 browser.
- ChromeDriver supports
google-chrome
when installed at the default location with respect to the underlying os: - 您可以将默认位置安装的
google-chrome
升级到当前的 Chrome版本79.0 级别. (根据 ChromeDriver v79.0发行说明) - Either you upgrade
google-chrome
installed at the default location to current Chrome Version 79.0 level. (as per ChromeDriver v79.0 release notes) Or you can override the default Chrome binary location i.e.
/usr/bin/google-chrome
with thechromium-browser
binary location following the documentation Using a Chrome executable in a non-standard location as follows:
1 对于Linux系统,ChromeDriver希望/usr/bin/google-chrome
是实际Chrome二进制文件的符号链接.
1For Linux systems, the ChromeDriver expects /usr/bin/google-chrome
to be a symlink to the actual Chrome binary.
有两种解决方案:
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客户端版本太旧,则将其卸载并安装最新版本的 Web客户端 GA.
- 进行系统重启.
- 以非root用户用户的身份执行
@Test
. - 始终在
tearDown(){}
方法中调用driver.quit()
以关闭&优雅地销毁 WebDriver 和 Web Client 实例. - Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
- If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
- Take a System Reboot.
- Execute your
@Test
as non-root user. - Always invoke
driver.quit()
withintearDown(){}
method to close & destroy the WebDriver and Web Client instances gracefully.
您可以在以下位置找到相关的详细讨论:
You can find a relevant detailed discussion in: