End-to-end Testing
How-tos & Guides
7 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
AI Assistant for Playwright
Code AI-powered test steps with the free ZeroStep JavaScript library
Learn more

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

1
2
3
/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

1
2
sudo apt install python3
sudo apt install python3-pip

Windows

Install Chocolatey and then run the following commands below

1
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.

1
pip install selenium # or pip3 install selenium
1
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:

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

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

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

Import the library and set up a browser driver:

1
2
3
4
5
6
7
# 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
1
from selenium.webdriver.common.by import By

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

1
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

1
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

1
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

1
2
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.

1
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:

1
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

1
suggestions[0].click()

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

1
assert re.search(query, browser.current_url)

Assert that the current page heading contains our search query

1
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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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.

Alternative to WebDrivers: Reflect

As learned above, getting started with Selenium in Python is not straightforward. In particular, you’ve to download and configure the right web driver based on your target browser. Even though Webdriver manager makes everything easier, that remains a cumbersome and tedious task. It would be great if you could create automated tests for all browsers without writing any code, wouldn’t it? Well, this is exactly what Reflect is all about!

Reflect is a no-code testing platform that allows to you create and run tests across all popular browsers in the cloud.

With Reflect, you can define automated tests for your web applications without having to configure any web drivers. Actually, you don’t need any coding experience. Thanks to its point-and-click UI, you can quickly create tests directly in the browser and then run them on desktop or mobile platforms in the cloud.

Using a cloud platform for automated cross-browser testing like Reflect means you don’t have to build and maintain your own testing infrastructure. That’s a hassle-free way to get the benefits of automated testing without the headache of managing a testing grid. Avoid writing complex code and wasting time in web driver configurations, try Reflect for free today!

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. Also, we saw how a powerful, no-code, cloud solution testing tool like Reflect can save you from any configuration issue. Hopefully you found this article useful!

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 © Reflect Software Inc. All Rights Reserved.