Steps to Install Selenium in Google Colab



Installing and configuring Selenium on Google Colab can be a challenging task, especially for beginners. However, with the right guidance, you can have Selenium up and running on your Google Colab notebook in no time. In this step-by-step guide, we will walk you through the process of installing Selenium, overcoming the challenges posed by the updated version of Ubuntu OS, and executing Selenium scripts successfully. Whether you need Selenium for web scraping, automated testing, or other web-related tasks, this tutorial will provide you with the necessary instructions to set up Selenium on Google Colab and start harnessing its power.

#scroll to the bottom for updated video

Step 1: Install Selenium

To get started, open your Google Colab notebook and install Selenium by running the following command in the command prompt:

 !pip install selenium 

This command will install the Selenium library on your Google Colab notebook, enabling you to utilize its powerful web automation capabilities.

Step 2: Install Chromium Browser and Driver in Google Colab

Since the updated version of Ubuntu OS on Google Colab no longer supports the traditional method of installing chromium-browser, we need to use an alternative approach. Copy and paste the provided commands into your Colab notebook to add the necessary sources and install the chromium browser and driver.

%%shell

# 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

Step 3: Create Selenium options before launching Selenium in Google Colab

After installing the dependencies, it's crucial to configure Selenium options to ensure smooth execution in the headless Google Colab environment. The provided Python code snippet demonstrates how to create the necessary options, such as making the webdriver headless and setting window size.

def web_driver():
    options = webdriver.ChromeOptions()
    # make sure the --verbose option is the first argument
    options.add_argument("--verbose")
    options.add_argument('--no-sandbox')
    # the chrome in linux only supports headless browser
    options.add_argument('--headless')
    options.add_argument('--disable-gpu')
    options.add_argument('--disable-dev-shm-usage')

    return webdriver.Chrome(options=options)

Now we can call this function to initiate our Chrome webdriver

Step 4: Execute your Selenium script

With Selenium installed and options configured, you are now ready to execute your Selenium script. The example script provided in this guide demonstrates a basic search operation on Google. Customize the script as per your requirements, and leverage Selenium's features to automate tasks, scrape data, or perform automated testing.

Here's an example script that will lauch selenium driver in google colab and search for the term "Python" on Google and print the results to the console:

driver = web_driver()
driver.get('https://www.google.com')
search_box = driver.find_element_by_name('q')
search_box.send_keys('Python')
search_box.submit()
results = driver.find_elements_by_css_selector('h3')
for result in results:
  print(result.text)
driver.quit()

This script will open Google, search for "Python", print the results to the console, and then quit the webdriver.

Conclusion

By following this step-by-step guide, you have successfully installed and configured Selenium on Google Colab. You now have the power to automate tasks, interact with web pages, and extract valuable data. Selenium, combined with the capabilities of Google Colab, offers a robust platform for web automation and testing. Experiment with different Selenium functionalities and explore the possibilities that this powerful tool provides. Good luck with your Selenium endeavors on Google Colab!

Unexpectedly exited with status code 1?

If you are getting exited with status code 1 watch the below video for updated version