0
0
Selenium-pythonHow-ToBeginner · 4 min read

How to Submit Form in Selenium: Simple Guide with Examples

In Selenium, you can submit a form by calling the submit() method on a form element or any element inside the form, or by clicking the submit button using click(). Both ways trigger the form submission in the browser.
📐

Syntax

To submit a form in Selenium, you typically use one of these methods:

  • element.submit(): Submits the form that contains the element.
  • submitButton.click(): Clicks the submit button to send the form.

The submit() method works on any element inside the form, not just the form tag itself.

java
driver.findElement(By.id("formElementId")).submit();

// or

WebElement submitButton = driver.findElement(By.id("submitButtonId"));
submitButton.click();
💻

Example

This example shows how to open a webpage, fill a text input, and submit the form using the submit() method.

java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class SubmitFormExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        WebDriver driver = new ChromeDriver();

        driver.get("https://www.example.com/login");

        WebElement username = driver.findElement(By.name("username"));
        username.sendKeys("testuser");

        WebElement password = driver.findElement(By.name("password"));
        password.sendKeys("password123");

        // Submit the form by calling submit() on an element inside the form
        password.submit();

        // Wait or check for next page or confirmation here

        driver.quit();
    }
}
Output
The browser opens https://www.example.com/login, fills username and password fields, submits the form, and then closes the browser.
⚠️

Common Pitfalls

Common mistakes when submitting forms in Selenium include:

  • Calling submit() on an element that is not inside a form, which causes no submission.
  • Not waiting for the page to load after submission, leading to flaky tests.
  • Using click() on a button that is not the actual submit button or is disabled.

Always ensure the element you call submit() on is inside the form, or use click() on the correct submit button.

java
/* Wrong way: submit() on element outside form */
WebElement unrelatedElement = driver.findElement(By.id("header"));
unrelatedElement.submit(); // This will not submit the form

/* Right way: submit() on input inside form */
WebElement inputInsideForm = driver.findElement(By.name("username"));
inputInsideForm.submit();
📊

Quick Reference

Here is a quick summary of form submission methods in Selenium:

MethodDescriptionWhen to Use
element.submit()Submits the form containing the elementWhen you have a form element or input inside the form
submitButton.click()Clicks the submit button to submit the formWhen you want to simulate user clicking the submit button
Actions.sendKeys(Keys.ENTER)Sends Enter key to submit formWhen form submits on Enter key press

Key Takeaways

Use element.submit() on any element inside the form to submit it programmatically.
Clicking the submit button with click() simulates real user action.
Ensure the element used for submit() is inside the form to avoid no action.
Wait for page load or confirmation after submission to avoid flaky tests.
Use the method that best fits your test scenario for reliable form submission.