iT邦幫忙

1

【已解決】Google Colab 無法執行 Selenium。報錯:Service chromedriver unexpectedly exited. Status code was: 1

  • 分享至 

  • xImage

問題原因:the colab system is updated from v18.04 to ubuntu 20.04 LTS recently

處理方式:

參考[metrizable] https://github.com/googlecolab/colabtools/issues/3347/
"Since Ubuntu 20.04+ no longer distributes chromium-browser outside of a snap package, you can install a compatible version from the Debian buster repository"

Colab安裝(請獨立一個儲存格執行以下指令):

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

Colab執行(測試OK):

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')
wd = webdriver.Chrome('chromedriver',options=chrome_options)
wd.get("https://github.com/googlecolab/colabtools/issues/3347")
wd.save_screenshot('test.png')

問題:

前幾天程式碼在Colab執行Selenium正常,但今天(2023/01/18~)運行會報錯:

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

程式碼參考如下方連結:
https://stackoverflow.com/questions/51046454/how-can-we-use-selenium-webdriver-in-colab-research-google-com

!pip install selenium
!apt-get update # to update ubuntu to correctly run apt install
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/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')
wd = webdriver.Chrome('chromedriver',options=chrome_options)
wd.get("https://www.webite-url.com")

請問在Google Colab上如何解? 謝謝

stackoverflow最近也有人遇到:

  1. https://stackoverflow.com/questions/75125487/selenium-chromedriver-not-working-with-google-colab-anymore-on-python-3-8-16
  2. https://stackoverflow.com/questions/75155063/selenium-use-chrome-on-colab-got-unexpectedly-exited

https://ithelp.ithome.com.tw/upload/images/20230118/20125021hb5NIx9ig3.jpg

https://ithelp.ithome.com.tw/upload/images/20230118/20125021ZnIzrnJCLx.jpg

froce iT邦大師 1 級 ‧ 2023-01-18 10:55:32 檢舉
https://stackoverflow.com/questions/51046454/how-can-we-use-selenium-webdriver-in-colab-research-google-com
Nesso iT邦新手 5 級 ‧ 2023-01-18 13:54:06 檢舉
嗨,我今天也有遇到這個問題,也爬文一個早上沒有解決辦法。
我認為應該是 colab 環境從 ubuntu 18.04 更新到 20.04 導致
昨天使用 bionic 108.0.5359.71-0ubuntu0.18.04.5 的 chromedriver 程式是正常運作的。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答