Selenium Python Tutorial: A Python Automation Testing Guide with examples

Himanshu Sheth

Posted On: October 1, 2021

view count214528 Views

Read time27 Min Read

Python is a programming language that needs no introduction! It is one of the most preferred languages when it comes to projects involving Machine Learning (ML), Artificial Intelligence(AI), and more. On a different battleground, the combination of Selenium Python is widely preferred as far as website automation is concerned.

As per Stack Overflow Developer Survey 2021, Python is the third-most-popular language after JavaScript & HTML/CSS. The prowess of Selenium and Python helps in automating interactions with the WebElements in the DOM (Document Object Model).

In a recent LambdaTest social media poll on preferred Python testing frameworks, Pytest emerged as the clear choice for 65% of participants. This underscores the framework’s strong standing in the testing community, emphasizing the need for dedicated resources and support.

Pytest

Source

Selenium with Python is a combination used for automating web browser interactions. It facilitates the creation of scripts to perform tasks like clicking buttons, filling forms, and navigating web pages. While Selenium provides the framework, Python acts as the programming language, making the process intuitive and efficient. This combination is especially popular for web application testing and web scraping.

unnamed (46)

Source

So how do you get started with Selenium and Python for test automation ? Well, this Selenium Python tutorial deep dives into the essential aspects of Selenium and Python, the learnings of which will be instrumental in your web automation journey.

Selenium with Python is a powerful combination for automating web testing and interaction. It empowers developers and testers to write scripts in Python to control web browsers, simulate user actions, and perform various testing tasks. Selenium provides a user-friendly API that enables seamless navigation, form filling, clicking buttons, extracting data, and more, all while mimicking real user behavior.

In case you do not have prior expertise with Selenium or want to know more about what is Selenium, make sure to check out our detailed Selenium WebDriver tutorial to gain insights into the Selenium automation framework.

What are Selenium Python Bindings

Selenium with Python is a powerful tool for automating browser testing and web applications. It enables seamless execution of test cases, including interactions like button clicks, form filling, and comprehensive site navigation. Simplify your testing process with Selenium and Python.

And Python Selenium bindings provide APIs using which you can write functional tests using the Selenium WebDriver. Like other Selenium language bindings, Selenium Python APIs can be leveraged to develop highly efficient tests that let you interact with the WebElements on the AUT (Application Under Test).

The bindings support local browser drivers (e.g., Chrome, Firefox, Internet Explorer, etc.) and provide a remote WebDriver that lets you connect to a remote cloud Selenium Grid.

How to configure Python and Selenium for Web Automation Testing

In this part of the Python Selenium tutorial, we cover the steps involved in setting up the development environment for Selenium automation testing with Python.

The prerequisite for Python Selenium installation is Python. So before proceeding with the further steps, make sure to install Python on your machine. As of writing this blog, the latest stable version of Python was 3.9.7.

Follow the below-mentioned steps to setup Selenium in Python:

Step 1: Install Python

Like me, if you have multiple versions of Python installed on your machine, you would need to use python3 for using Python 3.9.7. However, if you are on Mac, triggering the python command will use Python 2, whereas triggering the python3 command will use Python 3.9.7.

Step 2: Download and Install pip

To install Selenium Python, you need to make use of pip, the Package Management System for Python. Like Python 3, we would be installing pip3 so that we can use Python 3 for the development of the test scenarios.

Installing pip on Mac

1. Run the below command on the terminal for downloading pip on your machine:

2. Trigger the below command to install pip3 on your machine:

unnamed

3. Verify whether pip3 installation was successful by triggering the pip3 –version on the terminal.

Installing pip on Windows

1. For Windows, download get-pip.py on your local machine and run the below command to install pip:

2. You can confirm whether pip installation is successful (or not) by running the below command on the terminal:

unnamed (40)

Step 3: Download and install Selenium

Now that pip is installed successfully, it’s time to install Selenium in Python. First, the corresponding pip (or pip3) command is used for installing Selenium. Next, perform the below-mentioned steps depending on the operating system.

Installing Selenium on Mac

1. Since we are using Python 3.9.7 for testing, we would be using the pip3 command for installing Selenium in Python. Trigger the pip3 install selenium command on the terminal:

As seen above, Selenium 3.141 (the current stable version of Selenium) is successfully installed on the machine.

2. Run the command pip freeze | grep “selenium” to verify the Selenium version installed on the machine. In my case, there are multiple Selenium versions present on the machine, but that should not cause any problem with web automation testing with Selenium Python.

unnamed (38)

Installing Selenium on Windows

1. Run the command ​​pip install -U selenium on the command prompt (or terminal) to install Selenium on Windows.

2. Now that Selenium for Python installation is complete, run the following command to verify the status of the installation:

Shown below is the output of the above command, which indicates that the installation was successful.

Step 4: Install PyTest framework

PyUnit (or unittest), the default testing framework, is derived from the JUnit framework. Since it is available out-of-the-box, it can be leveraged for unit testing as well as Selenium test automation.

PyUnit, when used with Selenium, can also be used for leveraging parallel testing in Selenium. However, the biggest downside of PyUnit is the necessity of boilerplate code for the test implementation. Over & above, the framework also makes use of the camelCase naming convention that is popularly used in Java.

In case you are inquisitive to know more about the PyUnit framework for Python automation testing, make sure to check out our detailed PyUnit Selenium Python tutorial that covers all the aspects of the said framework. In this Python Selenium tutorial, we would be using the PyTest, a more widely used framework compared to PyUnit.

Installing PyTest on Mac

1. Run the command pip3 install pytest for installing the latest version of pytest.

As seen below, pytest-6.2.5 was successfully installed on the machine.

unnamed

2. Verify whether the corresponding version of the PyTest framework was installed successfully by triggering the following command pip freeze | grep “pytest” on the terminal.

Installing PyTest on Windows

1. PyTest on Windows can be installed by triggering the pip install -U pytest command on the terminal.

unnamed

2. Check whether PyTest installation was successful or not by running the command pytest –version on the terminal.

Also Read – Top Selenium Python Frameworks for Web Automation Testing

Step 5: Install Browser Drivers (Optional for Cloud Selenium Grid)

This step is only applicable for Selenium Python tests that have to be executed on the local Selenium Grid. The Selenium WebDriver architecture shows that Selenium client libraries interact with the web browser through its corresponding browser driver.

You need to download the browser driver(s) (in line with the browser and browser version) on the local machine for Selenium Python testing on a local Selenium. You should download and copy the browser driver to the location that houses the browser executable (e.g., Chrome.exe for the Chrome browser). By doing so, you would not be required to explicitly specify the browser driver path when instantiating the browser driver.

Shown below are the locations from where you can download the corresponding browser drivers:

Browser

Download Location

Opera

https://github.com/operasoftware/operachromiumdriver/releases

Firefox

https://github.com/mozilla/geckodriver/releases

Chrome

http://chromedriver.chromium.org/downloads

Internet Explorer

https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver

Microsoft Edge

https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

Since I would be performing the Python Selenium test on Chrome browser, I downloaded the Chrome Driver that matches the Chrome browser version. As shown below, I downloaded Chrome Driver (v 94.0.4606.61) from the link mentioned above.

unnamed (33)
unnamed (32)

It is recommended to place the browser driver (i.e., in our case, it is ChromeDriver) in the location where the browser executable is present.

unnamed (31)

For Mac

unnamed (30)

For Windows

With this, we are all set to run our first Selenium Python test on the local Selenium Grid. We would be using the PyTest framework for the demonstrations. Later in this Python Selenium tutorial, we will also cover how to run parallel tests on a cloud Selenium Grid (like LambdaTest) using the PyTest framework.

LambdaTest is a powerful cloud-based Selenium Grid, which makes it super simple to perform live interactive cross browser testing of your live and local websites and web apps on 3000+ online browsers running on a real operating system.

Start your Python web automation testing today.

How to run Web Automation Tests using Selenium and Python

Now that we have covered the essentials related to setting up Selenium in Python, let’s get our hands dirty by testing some real test scenarios. In this Selenium Python tutorial, we would run two test scenarios on a local Selenium Grid. Here are the details:

Test Scenarios

Test Scenario – 1 (Browser – Firefox)

  • Navigate to the URL https://lambdatest.github.io/sample-todo-app/.
  • Select the first two checkboxes.
  • Send ‘Happy Testing at LambdaTest’ to the textbox with id = sampletodotext.
  • Click the Add Button and verify whether the text has been added or not.

Test Scenario – 2 (Browser – Chrome)

  • Navigate to the URL https://www.google.com.
  • Locate the search box.
  • Send ‘LambdaTest’ to the search box.
  • Click on the link that contains the LambdaTest HomePage URL.

Project Setup

Before we deep dive into the implementation, we will do a quick walk-through of the setup we are using to implement the scenarios. I have taken a leaf out of the following PyTest tutorial video to set up the base for implementing and running Python Selenium tests:

I am using Visual Studio (VS) Code IDE and Poetry Package Manager (instead of pip) since it comes with all the necessary tools to manage the project in a more deterministic manner.

unnamed (29)

However, you can use alternative Python IDEs like PyCharm, Atom, Sublime Text, etc., instead of VS Code. Since we have already done the installation of Selenium and PyTest using pip, we are good to go to perform Selenium testing in Python.

Follow the below-mentioned steps for creating a Selenium with Python Project in VS Code:

Step 1

Create a folder named “python-tutorial,” which will contain the files and folders that could contain the test code.

unnamed (28)

Step 2 (Applicable only for Poetry Package Manager)

If the poetry package is not installed, please run the respective command to install the package:

  1. For Mac – pip3 install poetry
  2. For Windows – pip install poetry

Since we are using the poetry package manager, run the command poetry init on the VS terminal. This command creates the pyproject.toml configuration file. When creating the config file, choose the Python version available in your machine (i.e., Python 3.9).

unnamed (27)

We are installing the following packages using Poetry (via pyproject.toml):

  1. Selenium (v 3.141.59)
  2. PyTest (v 6.2.5)
  3. pytest-xdist (v 2.4.0)
  4. Autopep8
  5. Flake8

Autopep8 automatically formats Python code to conform to the PEP 8 style guide. Flake8 is a Python package that helps check the code base against coding style (PEP8), programming errors, etc. For installing the above packages, enter the relevant package name (e.g., selenium/pytest/autopep8/flake8).



unnamed

unnamed

Once all the packages are added to the TOML file, press Enter to complete the generation of the file.

To install the packages mentioned in the config file, run the command poetry install on the VS Code terminal. On completion, this will generate the files pyproject.toml and poetry.lock.

In the further section of this Selenium Python tutorial, we look at the implementation of the two test scenarios.

Implementation (Test Scenario – 1)

Implementation (Test Scenario – 2)

Code Walkthrough

Since the Selenium Python APIs being used are more or less the same in both the test scenarios, we are doing a joint code walkthrough.

Step 1: Import the Selenium WebDriver, pytest, Keys, and other relevant classes from Selenium.

Keys class in Selenium Python lets you perform relevant keyboard actions on the WebElements in the DOM. PyTest in Selenium Python is primarily used for unit testing. The framework is pretty exhaustive (compared to the unittest framework) and provides a mechanism for realizing parameterized testing with Python.

Step 2: The time module in Selenium Python provides numerous ways of suspending the execution for a specified time duration. For demonstration, we are using the blocking sleep() function, which is not considered to be Selenium best practices.

You can also use Selenium wait in Python to handle the loading of dynamic elements in a more efficient manner.

Step 3: Proper names (starting with test_) are assigned to the test methods in both the test files.

Test – 1

Test – 2

def test_lambdatest_google():

Step 4: The next step is to create an instance of the respective browser (i.e., Firefox for Test-1 and Chrome for Test-2) to execute the tests. In both cases, we have downloaded the browser driver in the location where the browser executable is placed in both cases.

In case you are planning to run test on Safari, make sure to check out our detailed blog that deep dives into How to run Selenium tests on Safari using Safari Driver

Test – 1

Test – 2

Step 5:

Test 1

The find element method in Selenium Python is used for locating the relevant WebElement using the NAME property. The URL under test is the LambdaTest ToDo Page.

The find element method in Selenium Python is used for locating the relevant WebElement using the NAME property. The URL under test is the LambdaTest ToDo Page.

This is how we located the WebElements (li1 and li2) using the Inspect tool in Chrome.


Test 2

Here the URL under test is the Google homepage. An exception is raised if the window title does not match the expected title.

The search box on the Google homepage is located using the find_element_by_name method. Here is how we fetch the details of the required WebElement using the Inspect tool in Chrome:

Step 6:

Test 1

The current window title is compared with the expected title (i.e., Sample page – lambdatest.com). Assert is raised if the window title does not match the expected one. Other than this Selenium Python tutorial, you can check out our detailed blog on asserts in Selenium Python to get a deeper understanding of how to use assertions while performing Python automation testing.

Test 2

Send Keys in Selenium is used to input the search term (i.e., LambdaTest) in the search box that was located using the find_element_by_name() method.

Submit method in Selenium is used for submitting the form in Selenium. There are other ways to achieve the same task but submit() is the most efficient of them all!

Step 7:

Test 1

A new item (i.e., Happy Testing at LambdaTest) is added to the ToDo list. Then, the required WebElement is located using the find_element method and the ID property.

A new item is added to the ToDo list using the send_keys() method in Selenium.

Test 2

The find element by XPath in Selenium is used for locating the relevant search result from Google. We are using the POM Builder, a Chrome AddOn that lets you extract the XPath of the desired WebElement with ease. You can also use SelectorsHub for Chrome to locate XPath, CSS Selector, and other web locators with a few clicks.

unnamed (17)

Now that the relevant search link is located, the click command in Selenium is used to click and open the requisite page.

unnamed (16)

Step 8:

Test 1

We perform a check if the newly added item is successfully added to the ToDo list or not. The element li6 is located using the NAME property.

The text of the corresponding WebElement is read using the text method. getText method in Selenium is popularly used when you intend to read text attributes of the WebElement. In this case, the method will return the text attribute of the li6 element (i.e., Happy Testing at LambdaTest).

unnamed (15)

The quit() method in Selenium is used to destroy the instance of the Firefox browser.

Test 2

Assert is raised if the current window title (i.e., LambdaTest homepage) does not match the expected title.

On completion of the test execution, the instance of the Chrome browser is destroyed using the quit() method.

Test Execution

Since we have two test files (i.e., test_lambdatest_todo.py and test_lambdatest_google.py), we run the tests in serial by triggering the following command on the terminal.

This will serially execute the tests (i.e., non-parallel execution). This is not a recommended execution practice since it will elongate the execution time. It is definitely not recommended for large-scale projects!

Here is the screenshot of the test execution:

Test – 1 Execution

unnamed (14)

Test – 2 Execution

unnamed

As seen below, the tests were executed successfully:

Also Read: Parameterization in PyTest with Selenium

This certification is for professionals looking to develop advanced, hands-on expertise in Selenium automation testing with Python and take their career to the next level.

Here’s a short glimpse of the Selenium Python 101 certification from LambdaTest:

How to run Parallel Selenium Tests with Python

In this section of the Selenium Python Tutorial, we will see how to perform parallel testing using Python with Selenium.

By default, PyUnit and PyTest frameworks support only serial test execution (i.e., there is no out-of-the-box support for parallel test execution). Parallel execution in Selenium provides a number of benefits, the major ones are accelerated test execution, improved product quality, and faster time to market.
However, it is relatively easy to run Selenium Python tests in parallel using the PyTest framework. All you need to do is install the pytest-xdist plugin.

Here are the various execution modes supported by the pytest-xdist plugin:

  • Multi-process load balancing – Multiple CPUs or hosts can be used for performing a parallel test run.
  • LooponFail – Tests are repeatedly executed in a sub-process.
  • Multi-platform coverage – Provides the flexibility to use multiple Python interpreters (e.g., PyTest, PyUnit, etc.) in the same test run.

Here are the steps to install pytest-xdist plugin to run Selenium Python tests in parallel:

  1. Mac: pip3 install pytest-xdist
  2. unnamed (11)

  3. Windows: pip install pytest-xdist

unnamed (9)

As shown below, the pytest-xdist plugin provides a number of command-line options to leverage the available CPUs for running Selenium Python tests in parallel.

unnamed (8)

In our case, we would be utilizing 2 CPU cores (through -n option) to run the two test scenarios in parallel. Here the parallel execution is done at the file level (i.e., test methods in two files are running in parallel).

where <"number_of_cpus"> = 2

Since we had already installed the pytest-xdist plugin through poetry, we did not run the pip command to install the plugin.

unnamed (7)

To run the two tests in parallel, run the following command on the terminal:

As seen in the execution snapshot, both the tests are executing in parallel on the local Selenium Grid.

unnamed (6)

Both the tests were successfully executed in close to 19 seconds.

unnamed (5)

Other than this Selenium Python tutorial, if you need more information about parallel testing in Selenium Python, make sure to check out our detailed PyTest tutorial that deep-dives into the integral aspects of parallel execution with PyTest in Selenium.

How to run Selenium Python tests on Cloud Selenium Grid

In this Selenium Python tutorial, we have seen that running Selenium tests in Python on a local Selenium Grid is a good solution, only if the number of test scenarios and test combinations are less. For example, consider a scenario where you have a Mac machine, and you need to run cross browser tests on (Internet Explorer + Windows 10) combination.

Well, testing on Internet Explorer is still not a thing of the past! But, over & above, maintaining an in-house Grid infrastructure that houses machines with different browsers, browser versions, and platforms installed is a costly proposition.

This is where cloud Selenium testing can play a major role in running Python Selenium tests on a range of virtual browsers, browser versions, and operating systems. LambdaTest is one such platform that provides a scalable, secure, and reliable cloud-based Selenium Grid infrastructure that lets you run Python Selenium tests at scale.

From a developer’s perspective, you just need to migrate from a local Selenium WebDriver to Remote Selenium WebDriver. This essentially means only a few lines of code change, that too from an infrastructural point of view!

Create an account to get started with cloud Selenium Grid on LambdaTest. Once you have created a profile, note the username and access key from the LambdaTest profile section. Next, you can generate the desired browser capabilities by choosing the browser and OS options from the Selenium Capabilities Generator.

For demonstrating the use of cloud Selenium Grid for running Selenium Python tests in parallel, we will port the earlier tests so that they run on the following combinations:

Test Case – 1 (Chrome – 92, Windows 10)

  • Navigate to the URL https://lambdatest.github.io/sample-todo-app/
  • Select the first two checkboxes
  • Send ‘Happy Testing at LambdaTest’ to the textbox with id = sampletodotext
  • Click the Add Button and verify whether the text has been added or not

Test Scenario – 2 (Safari – 14, macOS Big Sur)

  • Navigate to the URL https://www.google.com
  • Locate the search box
  • Send ‘LambdaTest’ to the search box
  • Click on the link that contains the LambdaTest HomePage URL

Shown below is the implementation where the tests would run on the LambdaTest Selenium Grid instead of the local Selenium Grid:

Implementation (Test Scenario – 1)

Implementation (Test Scenario – 2)

Code Walkthrough

As the core functional logic is unchanged, we would just focus on the changes we did to run the Selenium Python tests on LambdaTest Grid.

Here are the browser capabilities (generated using the LambdaTest Capabilities Generator) for the two test scenarios.

Test – 1

Test – 2

Instead of the local Selenium Grid, the tests are executed on the cloud-based Selenium Grid on LambdaTest. Hence, the combination of user-name & access-key (available in the profile section) are used for accessing the Remote Selenium Grid (i.e., @hub.lambdatest.com/wd/hub).

The RemoteWebDriver API uses the remote URL and browser capabilities generated using the capabilities generator.

Test – 1

Test – 2

The remaining part of the implementation remains unchanged. That’s all in this Selenium Python tutorial, we are all set to perform the parallel execution on the cloud Selenium Grid.

Execution

Run the following command on the terminal to run Selenium Python tests in parallel on the LambdaTest Grid.

To check the execution status, visit the Automation Dashboard on LambdaTest. As seen below, the two tests are running in parallel on the cloud Selenium Grid. Since my current plan supports five parallel sessions, I could have run five parallel tests (if required).

unnamed

As seen below, both the Selenium tests in Python were executed successfully on the LambdaTest Grid.

unnamed

Bonus Time!

Selenium 4 RC2 release is definitely a moment of reckoning for the entire Selenium community! I am extremely excited to try out the latest release of Selenium 4.

Let’s see what future enhancements are in store for the Selenium Python users in the Selenium 4 framework. In case you want to unlock the answers to the what & how of Selenium 4, do check out our detailed Selenium 4 Learning Hub that deep dives into topics like relative locators in Selenium 4, geolocation testing with Python in Selenium 4, and more.

Ending this exhaustive blog with the food for thought for Selenium 4:)

It’s a Wrap

Get started with Selenium Python testing & that too free!!! Python is one of the widely used languages, and it is growing up the ranks with each passing year. Though PyUnit is the default test automation framework, a majority crowd prefer the PyTest framework for performing Python automation testing.

Also, it is relatively easy to run Selenium Python tests in parallel using the pytest-xdist plugin. In this exhaustive Python Selenium tutorial, we deep-dived into the integral aspects of running cross browser tests with Selenium in Python.

The potential of Python and Selenium can be truly exploited by performing Python automation testing on a cloud Selenium Grid (like LambdaTest). Furthermore, this approach helps in improving the product quality by increasing the overall browser coverage.

I hope you find this detailed Python Selenium tutorial useful! Also, do drop in your comments about the common challenges that you normally encounter when it comes to Selenium automation testing!

Happy Testing 🙂

Frequently Asked Questions

What is Python Selenium used for?

Selenium is a platform that helps in the creation of robust and stable automated tests for all browsers and platforms. Python is the language used with Selenium. It uses the Python API for easy scripting to traverse links, fill out forms, submit data, and more.

Is Selenium Python easy to learn?

Python is a powerful and easy-to-learn programming language for a wide range of applications. Combining Selenium and Python enables you to write test scripts concisely and effectively.

Is Selenium with python in demand?

Python and Selenium WebDriver together make a strong combination for testing web applications and are popular among developers. Python code can be used to connect UI Unit tests and Selenium WebDriver with the help of API. In addition, the bindings provide the foundation for automating complex web-based interfaces, eliminating much of the work normally involved in structuring tests to meet specific test objectives.

What level of Python is needed for selenium?

Selenium is a language-independent testing framework, which means all you need to write test cases in Python is basic knowledge of the language.

How to install Selenium Python?

To install Selenium for Python, use the command “pip install selenium” in the terminal. This will download and install the Selenium package, along with its dependencies, for Python.

How to use Selenium Python?

To use Selenium with Python, import the Selenium package in your Python script using the statement “from selenium import webdriver”. Then, create a WebDriver instance, navigate to web pages, locate elements, and perform various interactions using Selenium’s API.

What is Selenium Python?

Selenium with Python is a powerful automation testing framework that allows developers to control web browsers programmatically. It provides a Python interface to interact with web elements, perform actions, and automate web testing tasks.

How to import Selenium in Python?

To import Selenium in Python, use the statement “from selenium import webdriver” at the beginning of your Python script. This enables you to access Selenium’s functionalities and methods.

How to handle popups in Selenium Python?

To handle popups in Selenium with Python, you can use the WebDriver’s methods like "switch_to.alert" to switch the control to the popup and then accept, dismiss, or enter text into the popup as required.

How to handle stale element exceptions in Selenium python?

To handle stale element exceptions in Selenium with Python, you can use exception handling techniques. Wrap the code that interacts with the element in a try-except block, and if a stale element exception occurs, you can perform necessary actions like refreshing the page and retrying the element interaction.

Author Profile Author Profile Author Profile

Author’s Profile

Himanshu Sheth

Himanshu Sheth is a seasoned technologist and blogger with more than 15+ years of diverse working experience. He currently works as the 'Lead Developer Evangelist' and 'Senior Manager [Technical Content Marketing]' at LambdaTest. He is very active with the startup community in Bengaluru (and down South) and loves interacting with passionate founders on his personal blog (which he has been maintaining since last 15+ years).

Blogs: 132



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free