Selenium Chromedriver在Python 3.8.16上无法再与Google Colab配合使用了吗?

4
我过去一年都在谷歌笔记本上使用Selenium Chromedriver,它似乎完美地运行着。
但是上周,这个脚本似乎不再工作了。我查看了谷歌笔记本的Python版本,现在是Python 3.8.16,我认为这是代码出错的罪魁祸首。
我使用的代码是:
!pip install selenium
!apt-get update # to update ubuntu to correctly run apt install
!apt install -y chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin

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',options=chrome_options)`

现在在这一行中:

driver= webdriver.Chrome('chromedriver',options=chrome_options)

我收到一个错误,说:

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

有人已经找到了解决方法吗?


2
越来越多的人遇到了这个问题。但是到目前为止,没有一个答案对我起作用。如果您找到了解决方案,请告诉我。 - user3347814
这个教程对我很有帮助。 - user3347814
这个教程对我很有帮助。 - undefined
4个回答

1

运行这段代码。对我有效。

%%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

然后运行这个

!apt-get update
!apt-get install chromium chromium-driver
!pip3 install selenium

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

url = "http://example.com/"
options = Options()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
driver = webdriver.Chrome("chromedriver", options=options)
driver.get(url)
print(driver.title)
driver.quit()

你的答案可以通过提供更多支持性信息来改善。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案正确。您可以在帮助中心中找到有关编写良好答案的更多信息。 - Community
即使我运行了你的代码建议,我仍然遇到错误。WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 1 - BAM

0

似乎是chromedriver本身出了问题,而不是Python。

此外,我无法重现这个错误。 看起来没问题

对我来说,执行似乎很好。

您可以检查一下您的代码中是否有其他可能会破坏它但您没有提供的内容。


0
截至2023年10月,这在Colab上可以工作。
!pip install selenium
!apt-get update 
!apt install chromium-chromedriver
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=options)
url = "https://www.google.com"
driver.get(url)
print(driver.title)

请注意,在webdriver.Chrome(options=options)中没有'chromedriver'的输入。

0
你可以使用Google-Colab-Selenium
import google_colab_selenium as gs

driver = gs.Chrome()
# Your code to interact with the driver here
driver.quit()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接