由于服务chromedriver意外退出,Google Colab中的Selenium停止工作并显示错误,状态代码为:1

ddrv8njm  于 2023-01-26  发布在  Go
关注(0)|答案(1)|浏览(218)

几个小时前我在google colab中的 selenium 设置工作正常。现在它突然停止工作了。这是一个示例:

!pip install selenium
!apt-get update 
!apt install chromium-chromedriver

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',
                          chrome_options=chrome_options)

我得到错误:

WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 1

有什么解决办法吗?

cl25kdpy

cl25kdpy1#

此错误消息...

WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 1

...表示chromedriver服务意外退出。
这是因为colab系统最近从v18.04更新到ubuntu * v20.04 LTS * 时引发的问题。
主要原因是,Ubuntu * v20.04 LTS * google-colaboratory不再在快照包之外分发chromium浏览器

快速修复

@mco-gh按照@metrizable的指导(细节如下)创建了一个新的笔记本,截至目前,它运行得非常完美:https://colab.research.google.com/drive/1cbEvuZOhkouYLda3RqiwtbM-o9hxGLyC
溶液
作为一种解决方案,您可以使用@metrizable在讨论尝试在Colab中使用Chromedriver时的问题中发布的以下代码块,从Debian buster存储库安装 * chromium-browser * 的兼容版本

%%shell
# Ubuntu no longer distributes chromium-browser outside of snap
#
# Proposed solution: https://askubuntu.com/questions/1204571/how-to-install-chromium-without-snap

# Add debian buster
cat > /etc/apt/sources.list.d/debian.list <<'EOF'
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster.gpg] http://deb.debian.org/debian buster main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster-updates.gpg] http://deb.debian.org/debian buster-updates main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-security-buster.gpg] http://deb.debian.org/debian-security buster/updates main
EOF

# Add keys
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys DCC9EFBF77E11517
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 112695A0E562B32A

apt-key export 77E11517 | gpg --dearmour -o /usr/share/keyrings/debian-buster.gpg
apt-key export 22F3D138 | gpg --dearmour -o /usr/share/keyrings/debian-buster-updates.gpg
apt-key export E562B32A | gpg --dearmour -o /usr/share/keyrings/debian-security-buster.gpg

# Prefer debian repo for chromium* packages only
# Note the double-blank lines between entries
cat > /etc/apt/preferences.d/chromium.pref << 'EOF'
Package: *
Pin: release a=eoan
Pin-Priority: 500

Package: *
Pin: origin "deb.debian.org"
Pin-Priority: 300

Package: chromium*
Pin: origin "deb.debian.org"
Pin-Priority: 700
EOF

# Install chromium and chromium-driver
apt-get update
apt-get install chromium chromium-driver

相关问题