How to Automate Form Submissions with Python and Selenium

2021-01-26 16:33:02 | #programming #python #automation

Selenium gives us a variety of methods for populating and submitting forms. This is especially useful during QA, when you've staged a form and need to test all possible inputs. Rather than manually filling out the fields and checking boxes, by hand, you can instead provide Selenium with a CSV of sample data and autopopulate the form in milliseconds, as many times as needed.

You can even design a suite of test cases, to test dozens or hundreds of different scenarios. For example, you may want a scenario for signing up with with invalid credentials, signing up with an email address that has already been taken, logging in with invalid credentials, updating your profile, and so on. Selenium makes it easy to run the entire batch, saving you countless hours.

What's more, automated form submissions eliminate human error. Imagine if you had to carry out all of the steps we outlined, manually, everytime. One simple mistake could invalidate the entire test and risk pushing a bug into production, for users to discover. Depending on the type of service you're providing, this outcome could be disastrous.

This tutorial will teach you how to communicate with a localized version of the Selenium API, and write a Python program that fills out input fields, checks checkboxes and radio buttons, selects from a dropdown menu, submits a form, and validates the submission. All of these steps will run automatically, at the push of a button.

What Do I Need to Know Before Proceeding?

Make sure to complete the prerequisites, above, as this tutorial requires a project skeleton with VirtualEnv, Selenium, and a browser driver of your choosing. It's also important that you're familiar with navigating the HTML DOM tree, as we will be targeting specific elements with out automation code. Once completed, you should have the following folders and files:

▾ python-browser-automation/
  ▸ venv/
  ▾ drivers/
    chromedriver
    geckodriver
  main.py

To verify that the virtual environment is active, on Windows 10, make sure (venv) is in the PowerShell command prompt. For example, (venv) PS C:\Users\username\python-browser-automation>

To verify that the virtual environment is active, on Linux Ubuntu, make sure (venv) is in the terminal command prompt.

Automating Button Clicks and Keyboard Input

We touched on button clicks and keyboard input in a previous tutorial, but here, we're going to cover more precise actions that are required to interact with a variety of different form fields.

Understanding the HTML DOM Tree

Completing our HTML DOM Inspection tutorial is also essential in order to comfortably navigate and target DOM elements. The HTML DOM is a structured XML-like set of nodes with standardized node and attribute names you need to be familiar with.

The Complete Automated Form Submission Code

If you're in a hurry to get something running, copy the source code below into your main.py file at the root of the python-browser-automation directory and run it with python3 main.py. Or if Python 3 is your default version, just run python main.py.

The remaining sections of this tutorial explain each component in detail, so keep reading to learn more.

File: main.py

import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select


driver = webdriver.Chrome(executable_path='drivers/chromedriver')
driver.get('https://mock.codeboxsystems.com')


def scroll(target):
    if target is not None:
        driver.execute_script('arguments[0].scrollIntoView(true);', target)
    else:
        print('Could not scroll to %s' % target)


def find_element_by_id(id):
    try:
        WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.ID, id)))
        element = driver.find_element_by_id(id)
        return element
    except Exception as e:
        print(e)


def find_element_by_xpath(xpath):
    try:
        WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.XPATH, xpath)))
        element = driver.find_element_by_xpath(xpath)
        return element
    except Exception as e:
        print(e)


def unclick(element):
    if element:
        driver.execute_script('arguments[0].click();', element)
        is_selected = element.is_selected()
        if is_selected:
            driver.execute_script('arguments[0].click();', element)


def click(element):
    if element:
        driver.execute_script('arguments[0].click();', element)
        is_selected = element.is_selected()
        if not is_selected:
            driver.execute_script('arguments[0].click();', element)


def find_elements_by_name(name):
    try:
        WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.NAME, name)))
        elements = driver.find_elements_by_name(name)
        return elements
    except Exception as e:
        print(e)


def fill(element, value):
    if element:
        element.clear()
        element.send_keys(value)


def assert_text(element, value):
    if element is not None:
        text = element.text
        try:
            assert text == value
        except AssertionError:
            print('Failed with: %s' % text)


form = find_element_by_id('service-request-form')
scroll(form)

interest = Select(find_element_by_id('service-request-interest'))
interest.select_by_visible_text('IT Consulting')

email = find_element_by_id('service-request-sender')
fill(email, 'test@email.com')

work_phone = find_element_by_id('service-request-work-phone')
fill(work_phone, '1234567890')

company = find_element_by_id('service-request-company')
fill(company, 'Company')

country = Select(find_element_by_id('service-request-country'))
country.select_by_visible_text('United States')

first_name = find_element_by_id('service-request-first-name')
fill(first_name, 'FN')

last_name = find_element_by_id('service-request-last-name')
fill(last_name, 'LN')

project_description = find_element_by_id('service-request-project-description')
fill(project_description, 'Project description')

option1 = find_element_by_id('service-request-option1')
click(option1)

terms = find_element_by_id('service-request-terms')
click(terms)

submit = find_element_by_id('service-request-submit')
submit.click()

alert = find_element_by_id('service-request-success')
assert_text(alert, 'Success')

time.sleep(3)
driver.quit()

How to Target HTML Elements with Selenium

The following functions allow us to scroll elements into view, and target elements by either ID, XPATH, or their name attribute. Once the element is returned, we'll be able to fill, click, or run assertions against them. The scroll() function executes native JavaScript, whereas the other functions rely on Selenium's Expected Condition (EC) and waits up to 2 seconds to check for the visibility of the target element.

def scroll(target):
    if target is not None:
        driver.execute_script('arguments[0].scrollIntoView(true);', target)
    else:
        print('Could not scroll to %s' % target)


def find_element_by_id(id):
    try:
        WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.ID, id)))
        element = driver.find_element_by_id(id)
        return element
    except Exception as e:
        print(e)


def find_element_by_xpath(xpath):
    try:
        WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.XPATH, xpath)))
        element = driver.find_element_by_xpath(xpath)
        return element
    except Exception as e:
        print(e)
  

def find_elements_by_name(name):
  try:
      WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.NAME, name)))
      elements = driver.find_elements_by_name(name)
      return elements
  except Exception as e:
      print(e)

How to Fill Form Fields with Selenium

Here, we define a fill() wrapper function to make it easier to invoke Selenium commands for clearing out and populated an input field.

def fill(element, value):
  if element:
      element.clear()
      element.send_keys(value)

How to Click Buttons, Checkboxes, and Radio Buttons with Selenium

Once the target element is returned, we can use an all-purpose javascript click() to click buttons, and toggle checkboxes and radio buttons.

def unclick(element):
    if element:
        driver.execute_script('arguments[0].click();', element)
        is_selected = element.is_selected()
        if is_selected:
            driver.execute_script('arguments[0].click();', element)


def click(element):
    if element:
        driver.execute_script('arguments[0].click();', element)
        is_selected = element.is_selected()
        if not is_selected:
            driver.execute_script('arguments[0].click();', element)

How to Confirm that a Form was Submitted with Selenium

To ensure the form was submitted successfully, we just have to target the success alert that appears after the form submission, and assert that the text is equal to "Success".

def assert_text(element, value):
    if element is not None:
        text = element.text
        try:
            assert text == value
        except AssertionError:
            print('Failed with: %s' % text)

Conclusion

This tutorial covered how to use Python and Selenium to write an automated form submission bot. We learned how to fill out input fields, select from a dropdown, and click buttons, checkboxes, and radio buttons. We also learned how to verify that the form was submitted successfully.

Book Recommendations for You

Comments

You must log in to comment. Don't have an account? Sign up for free.

Subscribe to comments for this post

Want To Receive More Free Content?

Would you like to receive free resources, tailored to help you reach your IT goals? Get started now, by leaving your email address below. We promise not to spam. You can also sign up for a free account and follow us on and engage with the community. You may opt out at any time.



Hire Us for IT and Consulting Services









Contact Us

Do you have a specific IT problem that needs solving or just have a general IT question? Use the contact form to get in touch with us and an IT professional will be with you, momentarily.

Services

We offer web development, enterprise software development, QA & testing, google analytics, domains and hosting, databases, security, IT consulting, and other IT-related services.

Free IT Tutorials

Head over to our tutorials section to learn all about working with various IT solutions.

We Noticed Adblock Running

Because we offer a variety of free programming tools and resources to our visitors, we rely on ad revenue to keep our servers up. Would you consider disabling Adblock for our site and clicking the "Refresh Page" button?

Contact