提问人:Wayne Gao 提问时间:1/18/2023 最后编辑:undetected SeleniumWayne Gao 更新时间:2/10/2023 访问量:8842
Selenium 在 Colab 上使用 chrome 意外退出
Selenium use chrome on Colab got unexpectedly exited
问:
我已经在 google colab 中使用 selenium chromedriver 一段时间了。
今天我开始我的工作,我得到了以前从未有过的错误。
我想知道这个错误是否与 chromedriver 版本有关。
这是今天的安装日志
!apt-get update
!apt install -y chromium-chromedriver
Get:1 focal-updates/main amd64 apparmor amd64 2.13.3-7ubuntu5.1 [494 kB]
Get:2 focal/main amd64 liblzo2-2 amd64 2.10-2 [50.8 kB]
Get:3 focal-updates/main amd64 squashfs-tools amd64 1:4.4-1ubuntu0.3 [117 kB]
Get:4 focal-updates/main amd64 snapd amd64 2.57.5+20.04ubuntu0.1 [37.6 MB]
Get:5 focal-updates/universe amd64 chromium-browser amd64 1:85.0.4183.83-0ubuntu0.20.04.2 [48.3 kB]
Get:6 focal-updates/universe amd64 chromium-chromedriver amd64 1:85.0.4183.83-0ubuntu0.20.04.2
...
...
(Reading database ... 129791 files and directories currently installed.)
Preparing to unpack .../chromium-browser_1%3a85.0.4183.83-0ubuntu0.20.04.2_amd64.deb ...
=> Installing the chromium snap
==> Checking connectivity with the snap store
===> System doesn't have a working snapd, skipping
Unpacking chromium-browser (1:85.0.4183.83-0ubuntu0.20.04.2) ...
Selecting previously unselected package chromium-chromedriver.
Preparing to unpack .../chromium-chromedriver_1%3a85.0.4183.83-0ubuntu0.20.04.2_amd64.deb ...
Unpacking chromium-chromedriver (1:85.0.4183.83-0ubuntu0.20.04.2) ...
Setting up chromium-browser (1:85.0.4183.83-0ubuntu0.20.04.2) ...
update-alternatives: using /usr/bin/chromium-browser to provide /usr/bin/x-www-browser (x-www-browser) in auto mode
update-alternatives: using /usr/bin/chromium-browser to provide /usr/bin/gnome-www-browser (gnome-www-browser) in auto mode
Setting up chromium-chromedriver (1:85.0.4183.83-0ubuntu0.20.04.2) ...
Processing triggers for mime-support (3.64ubuntu1) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for libc-bin (2.31-0ubuntu9.9) ...
Processing triggers for systemd (245.4-4ubuntu3.19) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for dbus (1.12.16-2ubuntu2.3) ...
这是昨天的安装日志
Get:1 bionic-updates/universe amd64 chromium-codecs-ffmpeg-extra amd64 108.0.5359.71-0ubuntu0.18.04.5 [1,159 kB]
Get:2 bionic-updates/universe amd64 chromium-browser amd64 108.0.5359.71-0ubuntu0.18.04.5 [83.6 MB]
Get:3 bionic-updates/universe amd64 chromium-browser-l10n all 108.0.5359.71-0ubuntu0.18.04.5 [5,230 kB]
Get:4 bionic-updates/universe amd64 chromium-chromedriver amd64 108.0.5359.71-0ubuntu0.18.04.5 [5,594 kB]
...
...
Setting up chromium-chromedriver (108.0.5359.71-0ubuntu0.18.04.5) ...
Setting up chromium-browser-l10n (108.0.5359.71-0ubuntu0.18.04.5) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for mime-support (3.60ubuntu1) ...
Processing triggers for libc-bin (2.27-3ubuntu1.6) ...
我发现 colab 系统从 20.04 更新到 ubuntu 18.04。
这就是无法使用 chromedriver 的原因吗?
我应该怎么做才能解决这个问题?
谢谢。
答:
运行时状态显示“已连接”后,打开命令面板 (ctrl + shift + p) 并选择“使用回退运行时版本”
这应该可以解决这个问题!
编号: https://github.com/googlecolab/colabtools/issues/3347#issuecomment-1386349011
我们最近升级了 Ubuntu 版本,这可能是导致这种情况的原因。连接到运行时后,是否可以尝试从命令面板 (ctrl + shift + p) 运行“使用回退运行时版本”,然后查看您的笔记本之后是否正常工作?
评论
这些日志消息...
Get:1 bionic-updates/universe amd64 chromium-codecs-ffmpeg-extra amd64 108.0.5359.71-0ubuntu0.18.04.5 [1,159 kB]
Get:2 bionic-updates/universe amd64 chromium-browser amd64 108.0.5359.71-0ubuntu0.18.04.5 [83.6 MB]
Get:3 bionic-updates/universe amd64 chromium-browser-l10n all 108.0.5359.71-0ubuntu0.18.04.5 [5,230 kB]
Get:4 bionic-updates/universe amd64 chromium-chromedriver amd64 108.0.5359.71-0ubuntu0.18.04.5 [5,594 kB]
...
...
Setting up chromium-chromedriver (108.0.5359.71-0ubuntu0.18.04.5) ...
Setting up chromium-browser-l10n (108.0.5359.71-0ubuntu0.18.04.5) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for mime-support (3.60ubuntu1) ...
Processing triggers for libc-bin (2.27-3ubuntu1.6) ...
...是最近将 Colab 系统从 Ubuntu 20.04 LTS
更新到时引起的问题的结果。v18.04
快速修复
@mco-gh
按照 的指导创建了一个新笔记本 (详情如下)截至目前运行良好:https://colab.research.google.com/drive/1cbEvuZOhkouYLda3RqiwtbM-o9hxGLyC@metrizable
详
在讨论中,尝试在 Colab 中使用 Chromedriver 时出现的问题明确提到:@EvanWiederspan
我们最近升级了 Ubuntu 版本,这可能是导致这种情况的原因。 连接到运行时后,是否可以尝试运行“使用回退 运行时版本“(Ctrl + Shift + P)
@metrizable
在他的评论中进一步澄清:
我知道您想将 selenium 与 chromium-browser 一起使用 在 Ubuntu 20.04 LTS 下。虽然 YMMV,但自 Ubuntu 20.04+ 不再 将 chromium-browser 分发到 snap 软件包之外,您可以 从 Debian buster 存储库安装兼容版本
示例代码:
%%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
# Install selenium
pip install selenium
注意
通常,以下代码行也会安装快照:
sudo apt install chromium-chromedriver
如果没有,您必须使用以下命令手动安装 Snap:
sudo snap install chromium
评论
您可以使用此示例 colab 工作簿重新生成脚本。 原因是上次 Ubuntu 更新仅通过 snap 支持 chromium diver。
带有解释的帖子:https://github.com/googlecolab/colabtools/issues/3347#issuecomment-1397277515
直接链接到笔记本:https://colab.research.google.com/drive/1cbEvuZOhkouYLda3RqiwtbM-o9hxGLyC
评论
更好的解决方案是,除了您在 colab 中通常做的事情之外,还要先这样做,以使硒发挥作用:
!sudo add-apt-repository ppa:saiarcot895/chromium-beta
!sudo apt remove chromium-browser
!sudo snap remove chromium
!sudo apt install chromium-browser
注意:@Johannes Stephan 在此处提供的解决方案有效,但它会使浏览器崩溃很多并使其无法使用
完整代码:
!sudo add-apt-repository ppa:saiarcot895/chromium-beta
!sudo apt remove chromium-browser
!sudo snap remove chromium
!sudo apt install chromium-browser
!pip3 install selenium
!apt-get update
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
评论