0

I use Selenium in Python(Google Colab) for scraping.

Error occurred even though nothing was changed.

It worked fine yesterday.

Sometimes it works, sometimes it doesn't.

Why does this happen?

ERROR

---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-2-f1882ef81cb2> in <module>
     15 options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36')
     16 
---> 17 driver = webdriver.Chrome('chromedriver',options=options)
     18 driver.maximize_window()
     19 

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 chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
!pip install selenium
import time
import random
import datetime

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

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

options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36')

driver = webdriver.Chrome('chromedriver',options=options) #Error here
driver.maximize_window()


#skip the rest

It would be appreciated if you could give me some hint.

SamuraiBlue
  • 851
  • 2
  • 17
  • 44
  • 1
    Check the details in this [discussion](https://stackoverflow.com/a/75165082/7429447) – undetected Selenium Jan 19 '23 at 21:08
  • @undetected Selenium, Thank you for your comment. I tried `cat > /etc/apt/sources.list.d/debian.list <<'EOF'`, then return `/bin/bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF')`. Is this to be expected? – SamuraiBlue Jan 19 '23 at 23:26
  • @undetected Selenium, Thank you for your comment. It worked!! What I did was... 1. not execute first cell `!apt-get update` to `!pip install selenium`. 2. add new cell then copy and paste solution `%%shell` script in [https://stackoverflow.com/questions/75164313/selenium-in-google-colab-stopped-working-showing-an-error-any-solution/75165082#75165082]. – SamuraiBlue Jan 21 '23 at 22:17

3 Answers3

1

webdriver.Chrome() need to be passed service and optionally options parameter.
Here you passing a dummy 'chromedriver' string instead of service
For example this is my code, it works:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 30)

url = "https://stackoverflow.com/"
driver.get(url)

'C:\webdrivers\chromedriver.exe' is a path to chromedriver.exe on my machine, you need to use your path. I think it should be without .exe extension sinceyour computer is not Windows based.

Prophet
  • 32,350
  • 22
  • 54
  • 79
0

This solution work for me on colab

https://github.com/googlecolab/colabtools/issues/3347#issuecomment-1387453484

0

make this change. It worked for me

!which chromedriver
    #output: /usr/bin/chromedriver
driver_service = Service('/usr/local/bin/chromedriver.exe')
driver = webdriver.Chrome(service=driver_service, options=chrome_options)

code image

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Mukua
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 24 '23 at 12:49