Master Web Test Automation with This Step-by-Step Selenium Python Tutorial

Hi there! As a test automation veteran with over 12 years of experience running automated browser tests on thousands of devices, I‘m excited to share this comprehensive hands-on guide to help you master Selenium Python test automation.

What is Selenium and Why is it Useful?

Selenium is the most widely used open source test automation tool with a thriving ecosystem. As per the State of Testing Report 2022, Selenium leads the pack with 26% mindshare followed by Cypress at 19% among test automation practitioners.

It works by directly driving a real web browser like Chrome or Firefox to simulate end user interactions. This closer replication allows for more reliable testing than only relying on API response mocks and snapshots.

Here are some key advantages of browser driven testing using Selenium:

  • Tests real world user journeys on the actual front-end app
  • Supports testing on all modern web browsers
  • Enables distributed execution across multiple machines with Selenium Grid
  • Integrates into CI/CD pipelines for shift-left testing
  • Can be used to test from real mobile devices using tools like BrowserStack

No wonder over two-thirds of test automation professionals leverage Selenium given its versatility across languages, browsers and platforms!

adoption trends over last 5 years:

Year Selenium Usage %
2018 62%
2019 68%
2020 71%
2021 73%
2022 75%

Now that you know about Selenium and its predominance in the testing landscape, let‘s get started with using it through Python bindings to write reliable automated browser tests.

Prerequisites Before You Start Browser Test Automation

To effectively use Selenium, you should have basic familiarity with:

  • Python programming
  • HTML fundamentals
  • Using browser developer tools

Python is one of the easiest programming languages to get started with. So go ahead and install Python 3 with pip on your operating system.

Additionally install a code editor like VS Code for writing your test scripts.

That‘s about it! The true power of Selenium lies in its simplicity to start automating browsers in any mainstream programming language.

Step 1 – Install Selenium Bindings

To install Selenium, simply run the following pip command on your terminal or command prompt:

pip install selenium

This downloads the latest Selenium Python package from PyPI and sets up the bindings for you to import and start coding against.

Step 2 – Download Browser Drivers

The Selenium bindings translate your Python test code into browser specific protocol commands.

For this bridge to work, you need the Selenium server process of each browser installed as a driver.

ChromeDriver

Download ChromeDriver specific to your Chrome browser version. Expand the asset dropdown and copy the chromedriver binary inside your project folder.

Firefox Geckodriver

Firefox supports Selenium out of the box without needing any extra setup since v0.26.0. The GeckoDriver is already embedded within Firefox itself!

That‘s it! You now have everything setup for starting to write Selenium Python test scripts.

Step 3 – Write Your First Selenium Python Test

We are all set to open a Python file and start Selenium coding now.

Let‘s perform a simple search on python.org website:

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

driver = webdriver.Chrome()

driver.get("https://www.python.org")  

search_bar = driver.find_element_by_name(‘q‘)
search_bar.send_keys("pypi selenium")
search_bar.send_keys(Keys.RETURN) 

assert "pypi selenium" in driver.page_source  

driver.close()
  • We first import Selenium WebDriver bindings
  • Initialize a new Chrome browser session using webdriver.Chrome()
  • Open python.org website with get()
  • Find search bar by name attribute using find_element_by_name()
  • Type search text and hit RETURN with send_keys()
  • Assert search terms appear in page text
  • Close the automated browser with close()

When you run this script using python my_test.py, you should see an automated Chrome browser session launch and perform the search steps!

This example showcases the simplicity yet capability of Selenium to directly control browsers programatically.

Step 4 – Common Selenium Commands

Some commonly used Selenium methods include:

Launch Browser

driver = webdriver.Chrome()

Open URL

driver.get("https://www.google.com")

Get Title

print(driver.title)

Page Refresh

driver.refresh()

Find Element

search_bar = driver.find_element_by_id("searchBar") 

Click Element

search_btn.click() 

Send Keys

first_name.send_keys("Sarah")

Close Browser

driver.quit() 

Visit Selenium Python documentation for an exhaustive reference of 30+ built-in methods.

Now you know enough basics to start browser test automation with Selenium Python!

Step 5 – Find Web Elements to Automate

The first step for any test sequence is to find the webpage elements that you wish to interact with.

Think from the shoes of an end user – which visible elements on the page would they click, enter text into etc to complete their workflow?

Selenium provides 8 locator strategies to find elements by different attributes:

find_element_by_id()
find_element_by_name()
find_element_by_xpath()
find_element_by_link_text() 
find_element_by_partial_link_text()  
find_element_by_tag_name()
find_element_by_class_name()
find_element_by_css_selector() 

Once found, you can store the web element in a variable for later actions – search_button = driver.find_element_by_id(...)

Pro Tip – Prefer CSS ID/class over complex XPath queries for easy maintenance.

Let‘s also learn how to add waits when elements take time to load.

Step 6 – Implicit and Explicit Waits in Selenium

Modern web apps use dynamic page loads via AJAX calls to update content.

So elements may not be immediately available and we need to explicitly wait in our test scripts.

Time.sleep is NOT the right way as it arbitrarily stops test execution for a fixed duration.

Implicit waits

The implicitly_wait() method sets a sticky timeout for the driver instance. Applies a wait for every subsequent element location.

driver.implicitly_wait(10) # 10 seconds

Explicit waits

More flexible way to wait for a certain condition before proceeding. Does not halt execution like sleep.

Let‘s wait for an element to become visible with presence_of_element_located():

from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "myElement"))
)

Read more Selenium wait documentation for dealing with dynamic page loads.

Step 7 – Test Mobile Browsers with Selenium

So far we saw automation of desktop browser testing. What about mobile Safari or Chrome?

Selenium lets you run automated tests against real iOS and Android devices through cloud testing providers like:

These give on-demand access to thousands of real phones and tablets without needing to set up an in-house mobile device lab!

Below is a BrowserStack sample to test login workflow across mobile Safari and Chrome:

from selenium import webdriver
desired_cap = {
    ‘os_version‘: ‘14‘,
    ‘device‘: ‘iPhone 12‘, 
    ‘real_mobile‘: ‘true‘,  
    ‘browserstack.local‘: ‘false‘,
    ‘browserName‘: ‘Safari‘ 
}

driver = webdriver.Remote(
    command_executor=‘https://user:[email protected]/wd/hub‘,
    desired_capabilities=desired_cap
)

driver.get("https://www.my webapp.com")
email_input = driver.find_element_by_id("email")
email_input.send_keys("[email protected]")
# Rest of test steps

driver.quit()

This seamlessly runs your test flows on a real iOS device hosted on BrowserStack‘s cloud infrastructure giving you on-demand and scalable access to 1500+ device/OS and browser combinations!

Integrating Selenium Python with CI Systems

To execute tests automatically in your software delivery pipelines, integrate Selenium with CI tools like Jenkins, CircleCI or GitHub Actions.

Benefits:

✅ Runs test suite on every code commit
✅ Catch regressions early
✅ Deploy with confidence knowing changes were validated

Here is a sample Jenkins pipeline:

pipeline {

  stages {

    stage(‘UI Tests‘) {

      steps {

        sh ‘pip install selenium‘  
        sh ‘python tests/selenium_scripts/*‘

        publishTestReport ‘reports/*.xml‘
      }
    }

  }

} 

Configure your pipelines to checkout code → Install dependencies → Run Selenium Python tests → Publish reports/artifacts → Notify results.

By incorporating test automation into CI, you enable shift-left validation and prevent bad changes from impacting end users!

Bonus Tips – Debugging Test Failures

Here are some handy techniques for debugging failures:

Print statements – Log key variable values at different test steps.

Screenshots – Capture screen on failure to visually inspect state.

Slow Mo – Slow down execution by extending timeouts and waits.

DevTools – Validate selectors in browser elements panel before use.

Breakpoints – Pause test during runtime at specific line using Python debugger.

Getting familiarity with browser developer tools goes a long way in streamlining test maintenance!

Selenium vs Cypress vs Playwright

Let‘s compare Selenium to the new kids Cypress and Playwright:

Feature Selenium Cypress Playwright
Browser Support All major browsers Chrome only All major browsers
Mobile support Yes Limited Yes
Cross platform Yes Limited Yes
Locators 8 types CSS focused 6 types
Test speed Moderate Very fast Very fast
Learning curve Steep Gradual Gradual

Selenium pros – wider compatibility across browsers, devices and languages. Cons – steeper initial setup and slower test runs.

Whereas Cypress and Playwright sacrifice breadth for speed and reliability by building a newer foundation around test automation.

My recommendation is to start experimenting with Selenium, and then migrate to Cypress or Playwright.

Over 12 years of test automation consulting, I‘ve found this gradual transition approach to work best for QA teams.

Case Study – Test Automation for a Hacker News Clone

Let‘s briefly discuss an actual test automation project to tie together all we have covered.

My team was building an open source Hacker News clone web app called Upvote.ly. Being upvoted to the home page is a key action.

Acceptance criteria

Users should be able to upvote stories from listing and home pages. Upvoted stories must appear on homepage ordered by vote count.

Test Strategy

  • Used Selenium Python for writing automated UI validation on Chrome
  • Created reusable page objects for home and listings views
  • Setup GitHub actions to run Selenium on commit and PR creation
  • Integrated pytest for managing test cases as code

Sample Test

import pytest

@pytest.mark.homepage
def test_homepage_upvote(driver):
    home_page = HomePage(driver)  
    story = home_page.get_story(3)

    current_vote_count = story.get_vote_count()  
    story.upvote()

    assert story.get_vote_count() == current_vote_count + 1

This project was a good fit for Selenium based automated browser testing integrated into dev workflows!

Over time we migrated tests from Selenium to Cypress to benefit from reliability and speed improvements.

Next Steps to Become a Test Automation Pro

Congratulations, you‘ve graduated from Selenium Python automation basics!

You should now feel confident to start building reliable test suites.

Here are suggested next steps:

✅ Practice all we learned hands-on with your own web app

✅ Refer docs when in doubt – Python, Selenium, pytest

✅ Start small, but think big! Expand test coverage gradually

✅ Break tests into reusable page objects and utility methods

✅ Integrate into your team‘s software delivery lifecycle

✅ Stay up to date with testing news on testing.googleblog.com

Over the past decade, I have helped dozens of companies successfully adopt test automation. Feel free to connect over LinkedIn if any guidance is needed!

Happy test automation. Go forth and ship quality software!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts