The selenium Package

The selenium package provides capabilities for automating the process of browsing the web and interacting with elements on any web page.

Reference:

Prerequisites

Before proceeding, we need to install a special kind of web browser called a "web driver" which the Selenium package will be able to control.

There are options to use a Firefox-based browser, but the professor recommends you install the Google Chrome-based "Chromedriver". Identify the path to where you have installed chromedriver. We'll need this value later (see CHROMEDRIVER_PATH variable below in the "Usage" example).

Installation

After installing a web driver, install the package using Pip, as necessary:

pip install selenium

Usage

First, initialize a new driver object. You can do so in the default mode, which will open a browser window for you to view, or in "headless" mode, which will not open a browser window.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

#
# INITIALIZE THE DRIVER
#

CHROMEDRIVER_PATH = "/usr/local/bin/chromedriver" # (or wherever yours is installed)

driver = webdriver.Chrome(CHROMEDRIVER_PATH)

# ... OR IN "HEADLESS MODE"...
# options = webdriver.ChromeOptions()
# options.add_argument('--incognito')
# options.add_argument('--headless')
# driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)

After initializing a driver, use it to visit and interact with web pages:

Resulting screenshots:

Last updated

Was this helpful?