submit()

Introduction

The submit() method is one of Selenium’s WebElement commands used to submit an HTML form. It performs the same action as pressing the Enter key inside a form field or clicking the form’s Submit button. When invoked on an element that belongs to a form, Selenium automatically submits the entire form to the server.

Although modern web applications commonly use JavaScript-based buttons with the click() method, the submit() method remains useful when working with traditional HTML forms. Understanding when to use submit() can help automation engineers write simpler and more maintainable test scripts.

In this tutorial, you’ll learn what submit() is, why it is used, its syntax, practical examples, real-world use cases, common mistakes, and best practices.


What is submit()?

The submit() method is a WebElement command that submits the HTML form containing the specified element.

Selenium first locates an element that belongs to a form and then submits the entire form automatically.

For example, consider the following HTML:

<form>
    <input type="text">
    <input type="password">
    <button type="submit">
        Login
    </button>
</form>

Selenium can submit the form using:

element.submit()

The submit() method is commonly used with:

  • Login forms

  • Registration forms

  • Search forms

  • Payment forms

  • Contact forms

  • Traditional HTML form submissions


Why Use submit()?

The submit() method is useful because it:

  • Submits entire HTML forms automatically.

  • Simulates pressing the Enter key.

  • Produces clean and readable automation scripts.

  • Improves automation maintainability.

  • Eliminates the need to locate separate Submit buttons in some scenarios.

  • Is useful when working with traditional HTML forms.


Syntax

element.submit()

Where:

  • element → Any WebElement that belongs to an HTML form.

  • submit() → Submits the form containing the specified element.


Example

The Selenium practice website contains a Login form with Username and Password fields. Selenium enters valid credentials and submits the form using the Password field.

The Selenium code is:

from selenium import webdriver
from selenium.webdriver.common.by import By


# Topic: 16. Basic WebElement Commands - submit()
# Practice site: https://the-internet.herokuapp.com/login
# Run: pytest -s 16_examples/test_04_submit.py
#
# submit() submits the form that contains the element, equivalent to pressing
# Enter or clicking the form's submit button.


def test_submit_form():
    driver = webdriver.Chrome()

    try:
        driver.get("https://the-internet.herokuapp.com/login")

        driver.find_element(
            By.ID,
            "username"
        ).send_keys("tomsmith")

        driver.find_element(
            By.ID,
            "password"
        ).send_keys(
            "SuperSecretPassword!"
        )

        driver.find_element(
            By.ID,
            "password"
        ).submit()

        assert driver.current_url.endswith(
            "/secure"
        )

    finally:
        driver.quit()

Output

The Login form is submitted
successfully using the
submit() method and the
browser navigates to the
secure page.

Understanding the Code

Import the By Class

from selenium.webdriver.common.by import By

Imports Selenium’s locator strategies.

Open the Login Page

driver.get(
    "https://the-internet.herokuapp.com/login"
)

Launches the Selenium practice website.

Enter the Username

driver.find_element(
    By.ID,
    "username"
).send_keys(
    "tomsmith"
)

Enters the username into the Username field.

Enter the Password

driver.find_element(
    By.ID,
    "password"
).send_keys(
    "SuperSecretPassword!"
)

Enters the password into the Password field.

Submit the Form

driver.find_element(
    By.ID,
    "password"
).submit()

Submits the entire Login form because the Password field belongs to that form.

Validate the Result

assert driver.current_url.endswith(
    "/secure"
)

Verifies that the Login operation is successful by validating the redirected URL.


How submit() Works

            Python Script
                   │
                   ▼
            Locate Form Element
                   │
                   ▼
               submit()
                   │
                   ▼
          Locate Parent HTML Form
                   │
                   ▼
            Submit Entire Form
                   │
                   ▼
              Browser Navigation
                   │
                   ▼
             Perform Validation

Practical Example

Suppose you’re automating a Search page.

The webpage contains:

  • Search textbox

  • Category dropdown

  • Search button

After entering the search keyword, Selenium can use:

search_box.submit()

to submit the entire form without locating the Search button separately.

This produces simpler and more readable automation scripts.


Automation Testing Example

Consider an online banking application.

Customers must submit forms while performing operations such as:

  • Logging in.

  • Updating profile information.

  • Searching transactions.

  • Updating account details.

  • Making payments.

Professional automation frameworks may use the submit() method whenever traditional HTML forms are used during regression testing.


Real-World Example

Automation engineers frequently use submit() while automating:

  • Banking applications.

  • E-Commerce websites.

  • Healthcare portals.

  • CRM systems.

  • ERP applications.

  • SaaS products.

  • Search forms.

  • Registration forms.

  • Traditional HTML-based applications.

Although modern applications frequently use JavaScript-based interactions, the submit() method remains useful when working with standard HTML forms.


Advantages of submit()

  • Produces clean and readable automation scripts.

  • Simulates traditional form submissions.

  • Eliminates the need to locate Submit buttons in some scenarios.

  • Improves framework maintainability.

  • Works well with standard HTML forms.

  • Is simple and easy to use.


Limitations of submit()

  • Works only when elements belong to an HTML form.

  • Modern JavaScript-based applications frequently use click() instead.

  • Custom UI components may not support form submissions.

  • Some applications require button clicks that trigger additional JavaScript operations.


Common Mistakes Beginners Make

Using submit() Outside an HTML Form

Incorrect

element.submit()

when the element does not belong to a form.

Correct

password_field.submit()

when the element belongs to an HTML form.

Always verify that the target element is part of a form before using submit().


Using submit() for JavaScript Buttons

Modern applications frequently use:

button.click()

instead of:

element.submit()

because clicking the button may trigger additional JavaScript operations.

Use the most appropriate interaction method for the application’s behavior.


Ignoring Synchronization Issues

Always ensure that:

  • Elements are visible.

  • Elements are enabled.

  • Pages are fully loaded.

before performing form submissions.


Best Practices

  • Use submit() only with HTML forms.

  • Verify that the target element belongs to a form.

  • Prefer click() when applications rely on JavaScript-based interactions.

  • Validate successful navigation after form submissions.

  • Use synchronization techniques whenever appropriate.

  • Keep automation scripts simple and maintainable.


Conclusion

The submit() method provides a simple and elegant way to submit HTML forms during Selenium automation testing. Although modern web applications frequently rely on JavaScript-based interactions, submit() remains extremely useful when working with traditional forms and standard browser behavior.

Understanding how and when to use submit() correctly helps automation engineers build reliable and maintainable Selenium automation frameworks across a wide variety of applications.


Frequently Asked Questions (FAQs)

What is the submit() method in Selenium?

The submit() method submits the HTML form containing the specified WebElement.

What is the syntax of submit()?

element.submit()

Does submit() click the Submit button?

Not necessarily.

The submit() method submits the entire form and behaves similarly to pressing the Enter key inside a form field.

Should I always use submit()?

No.

Modern web applications frequently use JavaScript-based buttons that require the click() method instead.

Can submit() fail in Selenium?

Yes.

The submit() method may fail when:

  • The element does not belong to an HTML form.

  • The webpage uses JavaScript-based interactions.

  • Appropriate synchronization techniques are not used.


Key Takeaways

  • The submit() method submits the HTML form containing the specified element.

  • It behaves similarly to pressing the Enter key inside a form field.

  • Use it only with elements that belong to HTML forms.

  • Prefer click() when applications rely on JavaScript-based interactions.

  • Always validate successful form submissions after execution.

  • The submit() method remains useful for traditional HTML-based web applications.