2

I was running this code in Google Colab, it was working fine but suddenly got this error with chromedriver. I am new to python webscrapping.

Error:

WebDriverException                        Traceback (most recent call last)
<ipython-input-9-4595cedc713a> in <module>
      6 options.add_argument('--no-sandbox')
      7 options.add_argument('--disable-dev-shm-usage')
----> 8 wd = webdriver.Chrome('chromedriver',options = options)

3 frames
/usr/local/lib/python3.8/dist-packages/selenium/webdriver/common/service.py in assert_process_still_running(self)
    117         return_code = self.process.poll()
    118         if return_code:
--> 119             raise WebDriverException(f"Service {self.path} unexpectedly exited. Status code was: {return_code}")
    120 
    121     def is_connectable(self) -> bool:

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

Code:

!apt-get update
!apt install -y chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin

!pip install selenium

from selenium import webdriver
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome('chromedriver',options = options)

I am trying to use selenium and chromedriver to get data from a website.

FHAlex
  • 33
  • 1
  • 6
  • same problem as here https://stackoverflow.com/questions/75155063/selenium-use-chrome-on-colab-got-unexpectedly-exited – sound wave Jan 18 '23 at 16:24

2 Answers2

0

You need to pass the path to chromedriver in the service initialization

from selenium.webdriver.chrome.service import Service
...
...
webdriver.Chrome(service=Service('/path/to/chromedriver'), options=options)

In older selenium version you can also use wd = webdriver.Chrome('chromedriver',options = options) as executable_path='/path/to/chromedriver'. So it should look like

wd = webdriver.Chrome(executable_path='/path/to/chromedriver',options = options)

You can also refer to the answers here - WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 127

Manish
  • 484
  • 3
  • 12
  • 1
    `executable_path` is deprecated in new versions. Use `webdriver.Chrome(service=Service(chromedriver_path), options=options)` where `from selenium.webdriver.chrome.service import Service` – sound wave Jan 18 '23 at 15:54
0

You can use this sample colab workbook to rebuild your script. The reason is that the last Ubuntu update update supports chromium diver just via snap.

Post with explanation: https://github.com/googlecolab/colabtools/issues/3347#issuecomment-1397277515

Direct Link to Workbook: https://colab.research.google.com/drive/1cbEvuZOhkouYLda3RqiwtbM-o9hxGLyC

This should help you out.

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