How To Automate Filling In Web Forms With Python Using Selenium

Paulo Oliveira

Posted On: July 13, 2022

view count549240 Views

Read time17 Min Read

Test automation has become essential in all software development projects due to agile business needs that increasingly require faster development and delivery. Also, automating frontend tests has become commonplace in the daily lives of software quality professionals.

According to Stack Overflow Developer Survey 2021, Python passed SQL and became the third most popular technology, making Python an even more important language for software development and automation testing.

In this Selenium Python tutorial, we are going to learn how to automate filling in web forms with Python, starting from the prerequisites needed to install and configure your environment to running the tests directly on cloud Selenium Grid like LambdaTest.

So, let’s get started!

Why Python for Test Automation?

Python was created to be a simple, open-source with excellent readability and a cross-platform language (it can run on any operating system and device).

When writing this blog, the Python language is in the 3.10 version. Python is a programming language that is constantly being updated and has excellent community support.

According to Stack Overflow Trends, Python has gained prominence over the years and has become the most popular language, as seen in the chart below.

Stack Overflow Trends

Using Python in software development and test automation is an excellent choice. Besides the fact that it makes your activities easier given that it is a simple and easy-to-use language, it will also allow you to be in touch with the most popular language in the world nowadays, which gives a lot of prominence to any IT professional.

Python is one of the most used programming languages for test automation, and there are many reasons for it. If you want to know more about it, you can check this blog on why Python is my favorite for test automation.

Python automation testing with Selenium is a killer combination for web automation testing. Selenium is open-source and one of the most popular test automation frameworks that support six popular programming languages, including Python. When writing this blog on how to automate filling in web forms with Python, the latest version of Selenium is Selenium 4.

Let’s deep dive into some of the fundamentals of Selenium before demoing how to automate filling in web forms with Python.

What is Selenium WebDriver?

For starters, Selenium WebDriver is a set of APIs that allows automatic interaction between your test automation code and the web browser on which automation tests are performed. Selenium allows the automation of browsers by sending commands to it, making it an exciting framework for automating user interface tests.

Selenium WebDriver has always been one of the most used test automation frameworks by the software quality community, mainly because of the wide range of available features and the wide range of programming languages supported ​​(such as Python, JavaScript, Java, C#, PHP, and Ruby).

Selenium is currently on version 4, which improved the Selenium Grid architecture, relative locators, and W3C compliance of Webdriver APIs. If you want to learn more about what is new in Selenium 4, check out this Selenium 4 tutorial.

You can also Subscribe to the LambdaTest YouTube Channel and stay updated with the latest tutorials around automated browser testing, Selenium testing, Cypress E2E testing, CI/CD, and more.

In the next section of this blog, we will learn how to automate filling in web forms with Python using Selenium with an example.

Demonstration: How to automate filling in web forms with Python using Selenium?

The primary purpose of this blog is to show you how to automate filling in web forms with Python using Selenium. We are going to do this by executing the following test scenario.

Test Scenario

  1. Open the Register Account form in the LambdaTest e-commerce playground.
  2. Fill in the personal details.
  3. Fill in the password.
  4. Subscribe to the newsletter.
  5. Accept the terms and conditions.
  6. Click the Continue button.
  7. Check that a success message is shown to the user.

You can have a quick view of the form where we will automate some actions in the picture below.

automate some actions

Prerequisites: Setup Process

To start our coding, we need to proceed with the following steps to set up Python, Selenium, Visual Studio Code, and ChromeDriver.

Step 1: Download and Install Python

First, you must download and install Python according to your operating system.

Step 2: Install Selenium WebDriver

Selenium WebDriver can be installed simply using the pip install manager. PIP is a package management system used to install/manage packages and libraries written in Python.

package management system

Step 3: Download and Install Visual Studio Code

Visual Studio Code (VSC) is one of the most used coding IDEs. For our coding demonstration, we are going to use Visual Studio Code. You are free to use your preferable IDE; everything that we will show in this blog on how to automate filling in web forms with Python will work in any IDE. Choose yours!

Step 4. Install WebDriver Manager for the Python library

Finally, you will need to install the driver that Selenium will use to interact with your browser. For example, ChromeDriver is used by Selenium WebDriver to control the Chrome browser.

In this blog, we will use the WebDriver Manager for Python, a library that simplifies the management of drivers for different browsers. Using the WebDriver Manager, we could automate tests for Chrome, Firefox, Internet Explorer, Opera, Brave, and Edge.

One significant benefit of using WebDriver Manager instead of directly using the specific browser driver is that we could deal better with browser versions. WebDriver Manager analyzes your browser version (always being automatically updated and not under your control) and uses the corresponding browser driver effortlessly and transparently.

WebDriver Manager for Python can also be installed simply using the pip install command.

pip install webdriver-manager

Starting: How to open a webpage on Chrome?

According to our test scenario, the first thing we need to do is “Open the Register Account form in the LambdaTest Playground website”.

First, you need to import the Selenium WebDriver and WebDriver Manager modules that are needed for Selenium WebDriver to use ChromeDriver to interact with the browser.

Then, you have to instantiate the driver in a variable called “browser”. In this case, we are instantiating the ChromeDriver, given that we will use it in the Chrome browser. Using WebDriver Manager for Python, you just need to use the below code.

Finally, you need to use the browser instance to access the desired webpage, using the function “get” and informing the URL of the website that should be opened.

Extra Tip 1: If you want to have a better view, you can maximize your browser window using the below code right after the driver instantiation.

Extra Tip 2: You should use the below code to close the browser window when finishing your test. This code must be the last line of your script.

For more tips, refer to our blog on 16 Selenium Best Practices for Efficient Test Automation to efficiently use Selenium WebDriver.

How to automate filling in web forms with Python?

Continuing in our test scenario steps, we need to fill in the personal details composed of first name, last name, e-mail, and telephone.

To interact with form fields and buttons, we need to:

  1. Locate the element in the page using the find element method in Selenium.
  2. Do an action with the located element.

To locate the element, we need to find a way to locate this element inside the page html code.

The easiest way to locate an element is to use the ID locator. However, not all elements have an ID. Also, you are free to use any other Selenium locators of your choice. We will see a different locator (XPath locator) usage when using the radio button, the checkbox, and the button in this form.

Let’s start taking a look at the First Name field. In the Chrome browser, you can right-click the First Name field and click on the Inspect option.

First Name field

The element is highlighted in the image, and we can see the element’s properties below.

For the First Name field, we can see that there is an ID “input-firstname”. So, we can use it to locate this element.

Before it, we need to import another Selenium function to allow us to locate elements. It is the By function. The code is shown below, and you can put it right after the other imports we did initially.

We should create a first_name variable to store this element. We use the browser instance to call the find_element method.

The method’s first parameter is the way that the element will be located, in this case, using the ID. So, we use the By function followed by the value ID. However, different types of locators can be used. You can check our Selenium Locators Tutorial to learn more about each type.

In complementing the find_element method needs, we must inform the value of the ID we are looking for. In this case, the ID is “input-firstname”.

The code should be as below.

Then, after locating the element, we need to interact with it. In this First Name field, we need to fill in with the user’s first name, so our action will be typing inside the field.

In order to do this, we need to use the function “send_keys” and inform what we want to type. In this case, we want to tipe “FirstName”.

The code should be as below.

In the same way, let’s take a look at the Last Name, E-mail, and Telephone elements.

Last Name, E-mail, and Telephone elements

Last name element properties are highlighted in the image and are shown below.

Last name element properties

E-mail element properties are highlighted in the image and are shown below.

E-mail element

Telephone element properties are highlighted in the image and are shown below.

For these three elements, we can also locate the element using its ID. We can proceed using the same logic as before for the three fields.

Create a variable to store each element.

Use the send_keys method in Selenium followed by the text you want to type in each field.

In the next step, we need to fill in the password and confirm it. Let’s take a look again at each element.

fill in the password and confirm it

Password element properties are highlighted in the image and are shown below.

Password element properties

Confirm password element properties are highlighted in the image and are shown below.

Again, for these two elements, we can locate the element using the ID. We can proceed creating a variable to store each element, and using the send_keys method followed by the text you want to type in.

Then, we need to subscribe to the newsletter. We just need to click on the Yes option of the radio button.

Let’s take a look in the newsletter Yes option element. Right-click on “Yes” and click in inspect.

newsletter Yes option element

Element properties are highlighted in the image and are shown below.

It is possible to see that we do not have an ID, so we need to use other types of locators.

Here we are going to use the XPath locator. XPath is also known as XML Path, which helps you navigate the HTML structure of a webpage and is one of the most commonly used locators for Selenium WebDriver.

For more information about using XPath, you can check our XPath Locators Cheat Sheet.

To locate the newsletter Yes option, we will also use the find_element method of Selenium WebDriver. The first parameter of this method will be By.XPATH, which says that we are now using the XPath to locate the element, and as the second parameter, the XPath value that we are going to locate, that in this case will be:

This means that we are looking for an element in the page, that is a “label” that has an attribute called “for” with the value “input-newsletter-yes”.

The code to store the element in a variable will look as below.

To click in the Yes option, we should use the action “click” in the element.

We now need to accept the terms and conditions by clicking on the checkbox.

Again, let’s take a look at the element properties.

accept the terms and conditions

Element properties are highlighted in the image and are shown below.

Without id property, we can again use XPath to locate the checkbox element and the action “click” to accept the terms and conditions. The code is shown below.

Finally, we can click on the Continue button.

In the same way, let’s look at page structure to find the element properties.
page structure to find the element properties

Element properties are highlighted in the image and are shown below.

We are again using XPath in Selenium to locate the “Continue” button, but instead of using the “for” attribute, it is not present in the button element, we are going to use the “value” attribute that has the value “Continue”. Then, we could use the “click” action again.

The code is shown below.

To check if the register was correctly done, we need to check that a success message is shown to the user. We are going to check the browser title that is shown below, highlighted in yellow.

browser title

We have the “assert” keyword that compares two values. The first value will be the browser title value. We can get the browser title value using the property title of the browser instance. The second value will be the expected value, which should be “Your Account Has Been Created!”.

The code should look like the below.

If you are a novice or a dedicated Python programmer who is looking to ignite the spark in your career and make a mark, you can take your career to the next level with the Selenium Python 101 certification from LambdaTest.

selenium-python-101

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

How to automate filling in web forms with Python on cloud Selenium Grid?

If you followed all the above steps, you should have a code to fill in the form we need. The next step that we are going to present in this section is how you can run this test on a cloud Selenium Grid like LambdaTest.

LambdaTest is a continuous quality testing cloud platform that helps developers and testers ship code faster. Cloud-based cross browser testing platforms like LambdaTest provides an online browser farm of 3000+ browsers and operating systems to perform Selenium Python testing at scale.

  1. The first step is to create a LambdaTest account.
  2. The second step is removing or commenting on the code line configuring the WebDriver Manager.

    The main difference between running the tests locally or on LambdaTest is to use the capabilities to configure the environment where the test will run.

    In this blog on how to automate filling in web forms with Python using Selenium, we are going to run the tests with the following characteristics:

    • Selenium 4
    • OS: Windows 11
    • Browser: Chrome
    • Browser Version: 103.0
    • Resolution: 1024 x 768
  3. You can get the capabilities code automatically generated for you in the LambdaTest Capabilities Generator.

    The generated code for our environment is presented below.

    You have to put this code just after the imports and fill in the variables with the proper information.

    Then you should configure these three variables.

  4. You can get the “Username” and “Access Token” from your LambdaTest Profile Section.
  5. Finally, you have to set the URL where the test will run, with a default format, and the code that will instantiate the driver in a variable called “browser”.

    All the code that we built before remains the same.

  6. You can run your test in LambdaTest by just typing the below command:
  7.  python main.py

    Attention Point 1: main.py is the name of our file. You can adjust with the filename that you created.

    Attention Point 2: As you could see in the console, desired_capabilities has been deprecated for Selenium 4, but it is just a warning, it continues working fine.

    For your reference, the complete code is available below.

    GitHub

    In the LambdaTest Dashboard, you can see the last execution, as shown below.

    LambdaTest Dashboard

    Then, clicking on the last execution, you are redirected to the complete test details information, where you can see all the detailed information about the execution, as you can see below.

    test details information

    To see a complete test summary, you can access the LambdaTest Analytics Dashboard. It presents a summary of all test results and shows both their overall status and the trend for each individual test result.

    LambdaTest Analytics Dashboard

    Through the LambdaTest Analytics Dashboard, you’ll be able to analyze each test case, recognizing the top-performing test scripts. You can also evaluate your test performance over time by looking at reports regarding passing and failing tests, from which you can measure the overall effectiveness of your test automation efforts.

    Conclusion

    In this blog, you could learn about how to automate filling in web forms with Python.

    It was presented why using Python for test automation and what is Selenium WebDriver. Using a demonstration scenario, we presented the setup process you should follow to configure all the required environments.

    You could learn the most basic thing in Python web automation: how to open a webpage in Chrome. Then, it was shown how to automate filling in web forms with Python involving different fields and use different locator types. Finally, it was presented on how to run automation tests on a cloud Selenium Grid using the LambdaTest platform.

    Frequently Asked Questions (FAQs)

    Can we automate web application using Python?

    You have learned that Python can do everything a web browser can and more. Perhaps you could use Python to automate some tasks. You could use it to control virtual servers in the cloud. You could write bots that interact with users or mindlessly fill out forms! Go forth and automate!

    What is Selenium Python automation?

    Selenium is an open-source automation testing framework that supports several programming languages, including Python, and can be used on all major operating systems, including Windows, Linux, and Mac OS X. Selenium Python automation is a test automation technique to perform automated testing using Selenium and Python.

Author Profile Author Profile Author Profile

Author’s Profile

Paulo Oliveira

Paulo is a Quality Assurance Engineer with more than 15 years of experience in Software Testing. He loves to automate tests for all kind of applications (both backend and frontend) in order to improve the team’s workflow, product quality, and customer satisfaction. Even though his main roles were hands-on testing applications, he also worked as QA Lead, planning and coordinating activities, as well as coaching and contributing to team member’s development. Sharing knowledge and mentoring people to achieve their goals make his eyes shine.

Blogs: 12



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free