---
title: Selenium Python Tutorial with pytest (Free Course + Certification)
description: A hands-on Selenium WebDriver tutorial in Python with pytest, from a
  fresh virtual environment to parallel runs on a cloud Selenium grid. Every demo
  in this video is a real run of the course code.
source_url:
  html: https://testingbot.com/resources/videos/selenium-python-tutorial-course
  md: https://testingbot.com/resources/videos/selenium-python-tutorial-course.md
---

# Selenium Python Tutorial with pytest (Free Course + Certification)

 Share on Facebook 

 Share on Twitter 

 Post on Reddit 

 Share link 

https://www.youtube.com/embed/-q6qBPdtXaE

A hands-on Selenium WebDriver tutorial in Python with pytest, from a fresh virtual environment to parallel runs on a cloud Selenium grid. Every demo in this video is a real run of the course code.

## What you'll learn

- Set up Python, Selenium and pytest, with Selenium Manager instead of driver downloads
- Locators with the By API, and how to pick ones that survive a redesign
- Explicit waits with WebDriverWait instead of time.sleep
- pytest fixtures in conftest.py, command-line options and parametrize
- Plain assert, screenshots on failure, HTML and JUnit XML reports
- A page object for the web form
- Parallel runs with pytest-xdist
- Running the same suite on Chrome, Firefox and Safari on the TestingBot Selenium grid, and reading the results in the dashboard

The full course, with code, quick checks and a knowledge check, is free and needs no account: [Selenium Python Tutorial with pytest](https://testingbot.com/resources/courses/selenium-python). Free TestingBot Selenium Python certification (pass the exam and run your suite in parallel on TestingBot; issued by TestingBot, not by the Selenium project): [certification details](https://testingbot.com/resources/courses/selenium-python/certification).

## Chapters

- 0:00 Course introduction
- 0:54 Selenium and pytest: how they fit together
- 1:19 Chapter 1: Set up Selenium with Python and pytest
- 1:54 Chapter 2: Selenium locators in Python: the By API
- 2:38 Chapter 3: Selenium waits in Python: WebDriverWait and time.sleep
- 3:26 Chapter 4: pytest fixtures for Selenium: conftest.py and scope
- 4:07 Chapter 5: Assertions, screenshots on failure and test reports
- 4:49 Chapter 6: Page objects in Python
- 5:24 Chapter 7: Run Selenium tests in parallel with pytest-xdist
- 6:00 Chapter 8: Run Selenium Python tests on the TestingBot grid
- 8:18 Chapter 9: Knowledge check

_The presenter is an AI avatar of Jochen Delabie (TestingBot), voiced by a stock AI voice._

## Video transcript

### Course introduction

Hi, I'm Jochen from TestingBot. This free course teaches Selenium WebDriver in Python, with pytest as the test runner. You'll need Python 3.10 or newer, some basic Python, and Chrome. No Selenium experience needed. Chapter by chapter, you build one small project. You pick locators that survive a redesign, and use explicit waits instead of sleeps. You move the browser into a pytest fixture, save a screenshot on every failure, and write HTML and JUnit reports. Then you add a page object, and run in parallel with pytest-xdist. In chapter eight, the same tests run on TestingBot's Selenium grid, on Chrome, Firefox and Safari. When you're done, you can earn the free TestingBot certification. Let's get started.

### Selenium and pytest: how they fit together

Before any code, one idea runs through the whole course. Selenium WebDriver drives the browser: it opens pages, finds elements, clicks and reads text, but it has no idea what a passing test is. pytest runs the tests: it finds them, runs fixtures around them and reports the results, but it knows nothing about browsers.

### Chapter 1: Set up Selenium with Python and pytest

Chapter one gets a browser opening from a pytest test. You create a virtual environment, and install pinned versions from requirements.txt. There's no driver to download: Selenium Manager finds the right ChromeDriver for you. Then a first test opens Selenium's web form, types into it and submits. Here it is. pytest dash q, from the project folder. Chrome opens, fills in the text field, submits, and closes. One passed.

### Chapter 2: Selenium locators in Python: the By API

Chapter two is about finding elements. Every lookup is find\_element or find\_elements with a By strategy: ID, name, CSS selector, XPath and more. find\_elements returns an empty list, which is how you check that something is absent. And the rule: bet on attributes that say what an element is, not where it sits. Four locator tests, each in a fresh Chrome window. ID, name, CSS, tag name, link text and XPath; then a select element, and an empty list for an absent element. Four passed.

### Chapter 3: Selenium waits in Python: WebDriverWait and time.sleep

Chapter three fixes the most common cause of flaky tests: timing. A box appears one second after a click. Looking for it straight away fails. time.sleep passes, but always costs two seconds and still guesses. WebDriverWait waits for a condition, and moves on the moment it's true. Here are all three, timed. Five tests, timed with durations. Without a wait, the box isn't there yet, and the test fails. The sleep version passes, but always takes two seconds. The explicit wait moves on as soon as the box exists, in just over one second. One deliberate failure, four passed, and the durations show the difference.

### Chapter 4: pytest fixtures for Selenium: conftest.py and scope

Chapter four moves the browser into a pytest fixture in conftest.py. Setup runs before yield, teardown after it, even when the test fails. Two command-line options pick the browser and headless mode, and parametrize runs one test with several inputs. Here's one set of tests on Chrome and on Firefox. Two browser options, chrome and firefox, both headless. Each test runs once per browser, and the browser is part of the test ID. The dropdown test runs three values on two browsers. Eight passed.

### Chapter 5: Assertions, screenshots on failure and test reports

Chapter five makes failures useful. pytest rewrites plain assert, so a failure shows both values and a diff. A small hook tells the fixture whether the test failed, so it can save a screenshot before the browser closes. And two flags write an HTML report and JUnit XML. Here's a test that fails on purpose. One test now expects the title Web form, with an exclamation mark. It fails, and the fixture saves a screenshot before the browser closes. pytest shows both values, and a diff. Here's the screenshot the fixture saved, and the HTML report.

### Chapter 6: Page objects in Python

Chapter six adds a page object. One class holds the form's URL, its locators, and the actions a user takes there. It gets the driver from the fixture, waits inside its methods, and never asserts: the test decides what's correct. When the markup changes, you fix it in one place. The same kind of tests, now written against the page object. Each test opens the page, fills it in and submits through the page methods. Five passed.

### Chapter 7: Run Selenium tests in parallel with pytest-xdist

Chapter seven runs the suite in parallel with pytest-xdist. One flag, dash n, starts several worker processes. The real work is keeping tests independent: every test gets its own browser, and nothing mutable is shared. Here are seven tests on two browsers, four at a time. Seven tests, on Chrome and Firefox, with four workers. Four browsers run at once, one per worker process. Fourteen tests passed in about nine seconds.

### Chapter 8: Run Selenium Python tests on the TestingBot grid

Chapter eight moves the suite to TestingBot's Selenium grid, and the tests don't change. With one flag, the fixture creates a remote driver instead of a local one. Each session is named after its test, grouped in a build, and reports pass or fail before the browser closes. Here it is, running for real. First, your credentials. In the TestingBot member area, open Settings, then Security. The API Credentials card has your key and secret, each with a copy button. Keep them out of your code. Export them as TB\_KEY and TB\_SECRET; the fixture reads them from the environment, and TB\_BUILD groups the run. In CI, store them as secrets. The smoke test on three browsers, two at a time, on the TestingBot grid. Chrome and Firefox on Windows 11 start first. There are two workers, so Safari starts as soon as one of them is free. Then Safari 18 on macOS Sonoma runs the same test. Three sessions, all passed. In the TestingBot dashboard, the build groups all three sessions: one test on Chrome, Firefox and Safari, and every one passed. How the build ran shows each session as a bar, one row per parallel slot. The more sessions you run in parallel, the sooner the whole build finishes: hundreds of tests in minutes instead of hours. The summary shows the wall time, and how busy your parallel slots were, so you can see when more slots would speed things up. Click a session to open it: the environment, here Safari 18 on macOS Sonoma, the duration, and every command Selenium sent, with its parameters. The video shows the browser: here, Selenium has typed into the form. And here the form is submitted, and the page says Received. Where a test runs is just capabilities: pick the browser, version and platform, set the screen resolution, and add options such as geolocation. The capabilities generator writes the code for you.

### Chapter 9: Knowledge check

That's the course. The last chapter is a knowledge check: ten questions, each with a short explanation. If you'd like something to show an employer, take the free TestingBot certification: pass the exam, and run your suite in parallel on the grid. Both are linked from the course page.

## Sidebar

### TestingBot Cloud Testing

Run automated, manual and visual tests on remote browsers and devices. Sign up for a free trial.

[Free Trial](https://testingbot.com/users/sign_up)

### Other videos

[![Appium Tutorial with WebdriverIO in JavaScript (Free Course + Certification)](https://testingbot.com/assets/videos/appium-webdriverio-course-e7519b511b317da2db984af8a9517de74535de8a33a055a89e9cd34980663951.jpg)](https://testingbot.com/resources/videos/appium-webdriverio-tutorial-course)

#### Appium Tutorial with WebdriverIO in JavaScript (Free Course + Certification)

A hands-on Appium tutorial with WebdriverIO, in JavaScrip...

[Watch video →](https://testingbot.com/resources/videos/appium-webdriverio-tutorial-course)

[![Cypress Tutorial in JavaScript (Free Course + Certification)](https://testingbot.com/assets/videos/cypress-course-a12eb8c833003156f19867fc7cc4c56c12120cdcfad9ca8c658ca392a6614a14.jpg)](https://testingbot.com/resources/videos/cypress-tutorial-course)

#### Cypress Tutorial in JavaScript (Free Course + Certification)

A hands-on Cypress tutorial in JavaScript: from npm insta...

[Watch video →](https://testingbot.com/resources/videos/cypress-tutorial-course)

[![WebdriverIO Tutorial in TypeScript (Free Course + Certification)](https://testingbot.com/assets/videos/webdriverio-course-b963320836ab60610065c7f987c171329c5bcf168b5525cee18c93c9236ce5ec.jpg)](https://testingbot.com/resources/videos/webdriverio-tutorial-course)

#### WebdriverIO Tutorial in TypeScript (Free Course + Certification)

A hands-on WebdriverIO tutorial for web apps, in TypeScri...

[Watch video →](https://testingbot.com/resources/videos/webdriverio-tutorial-course)

## Ready to start testing?
[Start a free trial](https://testingbot.com/users/sign_up)
