Selenium Python Tutorial for Beginners (Step-by-Step using WebDriver)

selenium with python tutorial

In today’s fast-paced digital world, automation has become an integral part of software testing. This Selenium Python tutorial will walk you through how to set up and use Selenium with Python for web automation. Selenium is one of the most preferred frameworks because it supports multiple programming languages, including C#, Java, Perl, Ruby, JavaScript, and Python.

Selenium in Python is an open-source toolset that efficiently lets you automate web applications. When combined with Python, a powerful and easy-to-learn programming language, Selenium becomes even more effective for testing.

This guide is a Python Selenium tutorial, a comprehensive introduction to Selenium with Python, which will help you begin with your first automation script.

What is Selenium?

Selenium is an open-source automation framework that helps automate web applications across different browsers and platforms. It offers a suite of tools that include-

  • Selenium WebDriverAutomates interactions with web applications.
  • Selenium IDE – It’s a browser extension that helps in recording and playback of tests.
  • Selenium Grid – It helps run multiple tests in parallel on multiple machines and browsers.

In this article, we will see how to use Selenium Python to automate tests using Selenium WebDriver Python.

Setting Up Selenium With Python

Before starting to write our automated test scripts, we need to set up our development environment. 

Installing Python

First, you need to ensure that Python is installed in your system. You may download its latest version from the official Python website

To verify the installation, open the terminal or command prompt and enter the command below:

 You will see the version of Python installed in your system.

Setting Up Selenium With Python

Installing Selenium

Next, you can install the Selenium package using the pip installer. For this, enter the command below in the terminal or command prompt:

install the Selenium package using the pip installer

You can check the version installed for Selenium using the command below:

installing selenium in python

Installing IDE- PyCharm

To start writing Python code, you would need an IDE. We will be using PyCharm Community Edition for this tutorial. You can follow the steps on the official website and download the IDE.

Writing Your First Selenium Python Script

Now that your development environment is ready, you are all set to write your first Selenium Python automation test. We will be writing a basic step where we will perform the following steps:

  1. Open Google Chrome.
  2. Navigate to www.google.com.
  3. Capture the title of the web page.
  4. Close the browser driver.

Let us look at the steps to start our first Selenium test script with Python.

  1. Upon launching PyCharm, navigate to File and then click on New Project.
PyCharm python
  1. Next, enter the desired name for the project and click on the Create button. You may also select the Location for your project.
AD 4nXcVrdEPM15baws6j5fmRsP98XVS96o9tj74O0pdokj3HwKH HMbHxmThDO1ZLLhFkvhGhC9vH7GvHjHuNY7OSJviswV94ailpAHGtGTfAwPFDQ8ThcUkUAg6UK2b6dwi11DwW0a?key=tFZOd7GJG haNCv2MkwOT R2
  1. Now you can create your first test in the .py file. All you need to do is right-click on your project and click on New and then select Python File.
create your First Python Selenium Script
  1. Once your file is created, you can start writing your first Python Selenium script step by step. Below is the code for the simple test case discussed above.

Now, run the test and see the results to see the Chrome browser launching, the page title being printed in the console window, and finally the browser instance closing down.

AD 4nXdDJkeYk9Xb ndHONWbi5X0o9dqh2SmJLdllTliyhubZIGWUY4Cheq3G9Emsn4TlxQQtQ7H9UzTxco2Z06sQvWTokCM2EBjV3OJWkNNncUvFcAGHTrRdRPq 0ZVNf8eqQnS oystg?key=tFZOd7GJG haNCv2MkwOT R2

Basic Selenium Commands Using Python

Opening a Web Page

We use the basic get() method to open a web page using Selenium.

Locating Web Elements

Actions and interactions on a web page can be performed only after locating the web elements. Suppose an element is defined as –

Selenium offers various locating strategies as discussed below-

  1. Finding elements by ID

Syntax-

Example-

  1. Finding elements by name

Syntax-

Example-

  1. Finding elements by XPath

Syntax-

Example-

  1. Finding elements by CSS Selector

Syntax-

Example-

Interacting With Web Elements

Once the elements are located, we can perform actions like clicking, sending input, or retrieving text.

  1. Clicking a button
  1. Entering text
  1. Retrieving text

Handling Dynamic Elements in Selenium Python

In most applications, some web elements appear dynamically, and to interact with them, Selenium in Python needs to use waits. To handle such elements, we can use implicit and explicit waits.

  1. Implicit Wait

The code below is for the webdriver to wait for 10 seconds before executing the next line of code. Note that once an implicit wait is applied, it is set for the life of the webdriver object.

  1. Explicit Wait

It waits for a defined condition to occur before proceeding further. In the code below, the webdriver will wait for 10 seconds for the element to match the criteria, and if no element is found, a Timeout exception will be thrown.

Handling Alerts, Frames, and Windows

  1. Handling Alerts
  1. Handling Frames
  1. Handling Windows

Taking Screenshots

The code below allows you to take a screenshot of the current page.

Using PyTest in Selenium Python

PyTest is a Python testing framework often used along with Selenium Python to write automation test scripts. It helps in efficient test execution and reporting. Let’s have a glance at how you can use it for the same test that we wrote above.

Installing PyTest

You can install PyTest on your Python environment by using the command below in the command line or terminal of your Python project.

Writing PyTest Test Case in Selenium Python

We will be using the same test case as we used for our first Selenium test using Python. We will just add verifying the page title in this test. The code would look like below-

Executing the Test

You need to run the command below in your IDE terminal to execute the test. Note that you can replace your test file name. Additionally, an HTML report will be generated for the test and added to the project path as shown in the screenshots below.

Terminal screenshot showing pytest command execution

AD 4nXdlcE BJ9vQHznAXLnbTthbrp5Vu2nKYC3qrPYY7vCiqq5q eilTQEK7qx7vrlfYaIXi9k6JPiuqugHF2xR0CkI cOPRIeJvRb3YH52uku4sES1RargjzGFlzmMBEmS9q4wzSp?key=tFZOd7GJG haNCv2MkwOT R2

AD 4nXeAGUpVRn dxSB6vVB WVzKqSSblomX8Iy2IJdwh OuOBJ5zcA6yiIBffYYM8kyeKiypsZVfLz oWvrDl Z9JD0Kr2FNKGP L3bTeleanX65blTUDF68p8EQ7uCtM4512ZBWcGE?key=tFZOd7GJG haNCv2MkwOT R2

If you’re following along, this Selenium Python tutorial ensures you understand not just the basic commands, but also how to structure tests for maintainability and efficiency before diving into frameworks like PyTest.

Best Practices for Selenium Automation Using Python

Selenium Python is an effective combination to automate your tests, but it is always better to consider certain best practices to make your automation framework more robust.

Some key best practices are:

  1. Instead of using time.sleep() or implicit waits, always prefer using explicit waits to synchronize test execution with web page loading.
  2. Optimize WebDriver management by avoiding launching a new WebDriver instance for every test. Instead, use PyTest fixtures for efficient test execution.
  3. Run tests in headless mode for faster execution.
  4. Use Page Object Model (POM) to organize test scripts by separating page elements from the test logic.
  5. Leverage Selenium Grid for parallel test execution to run tests on multiple browsers and environments, helping scale your test coverage.
  6. Capture screenshots upon test failures to assist with debugging.
  7. Use logging to log test execution for easy debugging.

Following these best practices in Selenium Python ensures your tests are maintainable, scalable, and easier to debug.

Conclusion

This Selenium Python tutorial showed you how to get started with Selenium with Python, from setting up Selenium WebDriver Python to writing your first automation script, handling waits, using PyTest, and applying best practices. With these skills, you’re ready to start building robust automation frameworks using Python and Selenium. We saw basic commands that can help navigate, locate elements, perform actions, handle dynamic elements, frames, windows, and alerts using Python with Selenium. By using the PyTest framework, you can increase the efficiency of your tests and build a robust test automation framework.

You are now fully equipped to start using Selenium with Python and automate your repetitive web tasks efficiently!