End-to-end Testing
How-tos & Guides
6 min read

Installing ChromeDriver and Python Selenium

A step-by-step guide for installing and running Selenium tests in Python that use the Chrome WebDriver.

Ovie Okeh
Published August 1, 2022

Introduction

Automated testing is an essential part of web development that massively increases productivity in a team. By outsourcing the repetitive task of testing our web applications to computers, we free up more time to work on shipping features while ensuring a bug-free experience for our users.

With this in mind, we’ll be exploring how to use one of the most capable automated testing tools, Selenium, to run a sample automated test using the WebDriver API on a live page.

By the end of this tutorial, we will:

  1. Install and configure a Chrome Selenium instance in WebDriver using Python
  2. Write two small automated tests for a search feature

Installing Python

To follow along, you’ll need basic programming knowledge and Python installed on your computer.

If you don’t already have Python installed on your machine, go ahead and run the following commands in your terminal.

Mac

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"`
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
brew install python

Linux

sudo apt install python3
sudo apt install python3-pip

Windows

Install Chocolatey and then run the following commands below

choco install python python -m pip install -U pip

Installing and configuring Selenium

Now that we have Python set up, we can go ahead and set up Selenium and WebDriver. This should be relatively straightforward as there are only two steps required to accomplish this. There are multiple methods available for installing Selenium depending on your programming language. You could download the executable directly but for this tutorial, we’ll be using pip to do so.

pip install selenium # or pip3 install selenium
python -c "import selenium; print(selenium.**version**)"

If this command throws an error, try using pip3 to install Selenium again.

Configuring a WebDriver browser

Next, we need to configure a browser driver for Selenium to run our tests in. A browser driver allows us to spin up an instance of any major browser like Chrome, Chromium, Firefox, etc.

There are multiple methods of setting up a browser driver, but to keep things simple we’ll use a third-party Python library called Webdriver Manager to get the correct driver and configure it.

Download the webdriver-manager library using pip:

pip install webdriver-manager # or pip3 install webdriver-manager

Create a test.py script and open it in your code editor of choice.

touch test.py code test.py # or open the file directly

Import the library and set up a browser driver:

# test.py
from webdriver_manager.chrome import ChromeDriverManager from selenium import webdriver from
selenium.webdriver.chrome.service import Service as ChromeService

browser = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install())) browser.implicitly_wait(5)

# Add tests here

Writing your first automated tests

We now have all we need to start writing automated tests using the WebDriver API in Python.

To demonstrate the concept, we’ll write two small tests against Wikipedia to verify that a user can search for a term from the homepage.

Sample Test — A user can type and get suggestions

Acceptance criteria:

  1. As a user, I can type a search term into an input.
  2. When I type into the input field, I get a list of suggestions
  3. Open up the test.py file and add the following import
from selenium.webdriver.common.by import By

Visit the Wikipedia homepage using the browser we instantiated in the previous section

browser.get("https://wikipedia.org/")

Note: To know which attribute and element to query, you need to have access to the website’s source code. Only run automated scripts on sites in the public domain or that you own.

Query the input field and type a sample search query

search_input = browser.find_element(By.ID, "searchInput") search_input.send_keys("Python")

Assert that we get a list of suggestions for the search query

suggestions = browser.find_elements(By.CLASS_NAME, 'suggestion-link') assert len(suggestions) > 0

Note: suggestion-link is the class name of the element that contains the title of a suggestion.

We can also assert that each suggestion actually contains the search query we’re searching for. This is done by looping over each suggestion and asserting that the title text of the suggestion contains our search query

for suggestion in suggestions: title_element = suggestion.find_element( By.CLASS_NAME, 'suggestion-highlight')
assert re.search(query, title_element.text)

Add a print statement at the end to confirm that the test passed and then kill the browser instance using the .quit method.

print("tests passed") browser.quit()

At this stage, we now have a simple automated test that types into an input field and verifies that we get suggestions as we type.

Run the test by opening up your terminal and running:

python <path_to_test_file>

If you see a test passed output on your terminal, congratulations, you just wrote a real automated test.

Let’s extend this test to explore more features of WebDriver.

Sample Test — Clicking on a suggestion takes me to the appropriate page

Acceptance criteria:

  1. As a user, clicking on a suggestion takes me to the appropriate page.

Note: You should continue from the previous step and add the following code before the print and browser.quit() commands

Click on the first suggestion

suggestions[0].click()

Assert that the browser navigates to the appropriate page by checking if the current page url contains our search query.

assert re.search(query, browser.current_url)

Assert that the current page heading contains our search query

page_heading = browser.find_element(By.ID, "firstHeading") assert re.search(query, page_heading.text)

At this stage, your test file should look like this:

from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
import re

browser = webdriver.Chrome(
    service=ChromeService(ChromeDriverManager().install()))
browser.implicitly_wait(5)

query = "Python"

browser.get("https://wikipedia.org/")

search_input = browser.find_element(By.ID, "searchInput")
search_input.send_keys(query)

suggestions = browser.find_elements(By.CLASS_NAME, 'suggestion-link')
assert len(suggestions) > 0

for suggestion in suggestions:
    title_element = suggestion.find_element(
        By.CLASS_NAME, 'suggestion-highlight')
    assert re.search(query, title_element.text)

suggestions[0].click()
assert re.search(query, browser.current_url)

page_heading = browser.find_element(By.ID, "firstHeading")
assert re.search(query, page_heading.text)

print("tests passed")
browser.quit()

Run the test again running python &lt;path_to_test_file> in your terminal and confirm that the text tests passed is displayed.

Next Steps

We’ve only briefly explored the features of WebDriver and the variety of ways it allows us to interact with our web pages and test critical user flows.

To recap, we installed the following packages:

  1. Python
  2. Selenium
  3. WebDriver Manager

We’ve also used various Python and WebDriver APIs to write two small automated tests. Hopefully you found this article useful!

Try Reflect: A modern cross-browser testing platform

Reflect is a no-code testing platform that lets you build and run tests across all popular browsers. Instead of building and maintaining your own infrastructure, using a cloud platform like Reflect allows you to get the benefits of automated cross-browser testing without the headache of maintaining an entire testing grid yourself.

Try it for free.

Get started with Reflect today

Create your first test in 2 minutes, no installation or setup required. Accelerate your testing efforts with fast and maintainable test suites without writing a line of code.

Copyright © 2022 Reflect Software Inc. All Rights Reserved.