Search code examples
pythonselenium-webdriverselenium-chromedrivergoogle-colaboratory

How to install a web driver in Google Colab to work with Selenium?


I want to run Selenium in Google Colab (Selenium: 4.8.2, Python: 3.8.10). But when installing the web driver I get an error:

AttributeError: 'Service' object has no attribute 'process'

My code:

!pip install selenium

from google.colab import drive
drive.mount('/content/drive')

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

path = "drive/MyDrive/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(service=Service(path))

My browser: Google Chrome 110.0.5481.178

Driver: Latest stable release: ChromeDriver 110.0.5481.77

OS: Windows 10

Why does such an error occur? What am I doing wrong? Thanks for the help!


Solution

  • I got this answer from this blog and it worked!

    https://nariyoo.com/python-how-to-run-selenium-in-google-colab/ [Python] How to run selenium in Google Colab

    !pip install chromedriver-autoinstaller
    
    import sys
    sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
    
    import time
    import pandas as pd
    from bs4 import BeautifulSoup
    from selenium import webdriver
    import chromedriver_autoinstaller
    
    # setup chrome options
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless') # ensure GUI is off
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    
    # set path to chromedriver as per your configuration
    chromedriver_autoinstaller.install()
    
    # set the target URL
    url = "put-url-here-to-scrape"
    
    # set up the webdriver
    driver = webdriver.Chrome(options=chrome_options)
    

    Now you can easily import other selenium library that you need and the driver will be worked. I hope it help!