4

I have been using selenium chromedriver in google colab for the past year and it seems to be working perfectly.

But last week, the script doesnt seem to be working anymore. I looked at the python version of google colab and it's now on python 3.8.16 which I think is the culprit of this code breaking.

I use the code:

!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)`

And now in this line: driver= webdriver.Chrome('chromedriver',options=chrome_options)

I get an error saying: WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 1

Anyone already found a fix for this?

KMLearner
  • 41
  • 1
  • 3
  • 2
    https://stackoverflow.com/questions/75155063/selenium-use-chrome-on-colab-got-unexpectedly-exited More people are having this problem. But so far none of the answers worked for me. Please let me know if you found a solution. – user3347814 Jan 20 '23 at 14:39
  • https://www.youtube.com/watch?v=VSO14hgo6Gs&ab_channel=PythonWebScraping This tutorial worked for me. – user3347814 Jun 21 '23 at 14:21

2 Answers2

1

Run This Codes. For me it Worked

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

Then Run This

!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()
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 02 '23 at 00:44
  • I'm still getting the error, even after running your code suggestion. WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 1 – BAM Feb 24 '23 at 05:10
0

Seems to be a problem with chromedriver itsself, and not python.

Also, I couldn't reproduce the error. Seems to be fine

The execution seems to be fine for me.

You might check, if there's something else in your code which might break it and you didn't provide here.

kaliiiiiiiii
  • 925
  • 1
  • 2
  • 21