Mastering Selenium With Python: A Complete Guide

selenium with python tutorial

In today’s fast-paced digital world, automation has become integral to software testing. Selenium is one of the most preferred tools among the various automation tools. Selenium is an open-source tool that efficiently lets you automate web applications. When combined with Python, which is an easy-to-learn and powerful programming language, Selenium becomes even more effective for testing. 

This guide is a comprehensive introduction to Selenium using Python which can 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 we can use Python to automate Selenium tests using Selenium WebDriver.

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 below command-

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

python version

Installing Selenium

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

install the Selenium package using the pip installer

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

AD 4nXcGScb509V07Gs ndYn VC7c7kQuEYe aSIGkq8veHWMjsjPVkXDdEgYnPQGE0OBPzF7hMMkO7BfsE1zRQ71PMSnrBUhE2uYJAOxywe9uQC3MJ dz42gQwsEP2KIYxugJWgT PLQ?key=tFZOd7GJG haNCv2MkwOT R2

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 Python Selenium Script

Now that your development environment is ready, you are all set to write your first Python Selenium 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
  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.
AD 4nXeDwo73IMcJDTkQIqhVKgQ3zD3vW2ia9RmRHbZYsKh7v GcRHhIfFFnr 7lshG 8tSsYfnYL5ufoTO589DGX d xx0 hJZu9dej 4N91gn9pKKZsV MFoZ4kTziVrhwm2z7V4U?key=tFZOd7GJG haNCv2MkwOT R2
  1. Once your file is created, you can start writing your first selenium script with Python. 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 most applications, some web elements appear dynamically, and for interacting with them Selenium needs to wait. To handle such elements, we can use implicit and explicit waits.

  1. Implicit Wait

The below code is for the webdriver to wait for 10 seconds before executing the next line of code. Note that once 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 below code, 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 below code allows you to take a screenshot of the current page.

Using Pytest With Selenium

PyTest is a Python testing framework that can be used to write your automation test scripts. It helps in efficient test execution and reporting. Let’s have a quick glance of 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 below command in the command line or terminal of your Python project.

Writing PyTest Test Case

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 below command 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.

AD 4nXfdMQF3Zl7NYxitqgx ORchtC18zcAvmfjxPJl30fc335thVshxhBpEWb1qLlOPgH7rhch7DkMWpbl7cMjgbLKxoW3FmsDlml2RMzbrIzWDaiZh3SpAFqttjqWjudUXZ7Ng HHfqw?key=tFZOd7GJG haNCv2MkwOT R2

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

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

Best Practices for Selenium Automation Using Python

Selenium using Python is an effective combination to automate your tests, but it is always better to consider certain best practices to make your tests even more effective. Some of these 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.
  7. Use logging to log test execution for easy debugging.

Conclusion

Selenium with Python is a powerful combination for test automation. We have covered the basics of Selenium along with Setting up Selenium and Python to start with test automation. 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!