0
0
Selenium Javatesting~15 mins

Submitting forms in Selenium Java - Deep Dive

Choose your learning style9 modes available
Overview - Submitting forms
What is it?
Submitting forms means sending the data entered by a user on a web page to the server for processing. In web testing, it involves automating the action of clicking the submit button or triggering the form submission event. This lets testers verify that the form behaves correctly and the server receives the data as expected. It is a key step in testing user interactions on websites.
Why it matters
Without form submission testing, errors in data sending or server handling can go unnoticed, causing broken user experiences or lost data. Forms are how users communicate with websites, like signing up or buying products. If submissions fail, users get frustrated and businesses lose trust and revenue. Testing form submission ensures reliability and smooth user journeys.
Where it fits
Before learning form submission, you should understand locating web elements and basic Selenium commands. After mastering form submission, you can learn about validating server responses, handling asynchronous waits, and testing complex form workflows like multi-step forms or file uploads.
Mental Model
Core Idea
Submitting a form in Selenium automates the user action of sending form data to the server by triggering the form's submit event or clicking its submit button.
Think of it like...
Submitting a form is like mailing a letter: you fill out the letter (form fields), then drop it in the mailbox (submit) to send it off for processing.
┌───────────────┐      ┌───────────────┐      ┌───────────────┐
│ User fills    │ ---> │ User clicks   │ ---> │ Server receives│
│ form fields   │      │ submit button │      │ form data     │
└───────────────┘      └───────────────┘      └───────────────┘
Build-Up - 7 Steps
1
FoundationUnderstanding HTML Forms Basics
🤔
Concept: Learn what an HTML form is and how it works in a web page.
An HTML form groups input fields like text boxes and buttons inside a
tag. The form has an action URL where data is sent and a method (GET or POST) defining how data is sent. The submit button triggers sending this data to the server.
Result
You can identify the form structure and understand what happens when a user submits it.
Knowing the form's HTML structure helps you target the right elements to automate submission.
2
FoundationLocating Form Elements in Selenium
🤔
Concept: Learn how to find form fields and buttons using Selenium locators.
Use Selenium methods like findElement(By.id), By.name, or By.cssSelector to locate input fields and the submit button. For example, driver.findElement(By.name("username")) finds the username field.
Result
You can programmatically access form inputs and buttons to interact with them.
Precise element location is essential to automate filling and submitting forms reliably.
3
IntermediateUsing WebElement.submit() Method
🤔Before reading on: do you think calling submit() on any element submits the form or only on form elements? Commit to your answer.
Concept: Learn how Selenium's submit() method triggers form submission from a form element or its child.
In Selenium, calling submit() on a form element or any element inside the form triggers the form submission. For example, driver.findElement(By.name("username")).submit() submits the form containing that input.
Result
The form is submitted without explicitly clicking the submit button.
Understanding submit() works on any child element helps automate forms without locating the submit button.
4
IntermediateClicking the Submit Button to Submit
🤔Before reading on: is clicking the submit button always better than using submit()? Commit to your answer.
Concept: Learn how clicking the submit button simulates real user behavior for form submission.
You can locate the submit button and call click() on it, e.g., driver.findElement(By.cssSelector("button[type='submit']")).click(). This mimics a user clicking the button, triggering any JavaScript tied to that event.
Result
The form submits, and any client-side scripts run as expected.
Clicking the button ensures JavaScript validations or events tied to the button click are executed.
5
IntermediateHandling JavaScript Form Submissions
🤔Before reading on: do you think submit() triggers JavaScript events attached to the submit button? Commit to your answer.
Concept: Learn that some forms use JavaScript to handle submission, requiring clicking the button instead of submit().
Some web forms use JavaScript event listeners on the submit button or form to validate or modify data before sending. Calling submit() may bypass these scripts, so clicking the button is necessary to trigger them.
Result
Form submission triggers all client-side scripts, ensuring realistic test behavior.
Knowing when to click vs submit prevents missing important client-side logic during tests.
6
AdvancedWaiting for Submission Results
🤔Before reading on: do you think form submission is always instant and the next page loads immediately? Commit to your answer.
Concept: Learn to wait for page loads or AJAX responses after submitting forms to avoid flaky tests.
After submitting a form, the page may reload or update parts asynchronously. Use Selenium waits like WebDriverWait with ExpectedConditions to wait for elements indicating submission success or page load before proceeding.
Result
Tests run reliably without errors caused by timing issues.
Proper waiting after submission is crucial to handle real-world delays and asynchronous behavior.
7
ExpertSubmitting Forms Without Visible Submit Buttons
🤔Before reading on: can you submit a form if the submit button is hidden or missing? Commit to your answer.
Concept: Learn advanced techniques to submit forms when no visible submit button exists.
Some forms submit on pressing Enter in a field or via JavaScript triggers. You can send Keys.ENTER to an input field or execute JavaScript like form.submit() via Selenium's JavascriptExecutor to submit the form programmatically.
Result
Forms submit successfully even without visible buttons, covering edge cases.
Knowing alternative submission methods helps automate tricky forms and improves test coverage.
Under the Hood
When a form is submitted, the browser collects all input field values inside the form and sends them to the server URL specified in the form's action attribute using the HTTP method (GET or POST). Selenium simulates this by triggering the submit event either via the submit() method or by clicking the submit button, which may also run JavaScript event handlers before sending data.
Why designed this way?
The submit() method was designed to provide a simple way to trigger form submission programmatically without needing to locate the submit button. However, because many modern sites use JavaScript to enhance or validate forms, clicking the button became necessary to trigger those scripts. Selenium supports both to cover different web designs.
┌───────────────┐
│ Selenium Test │
└──────┬────────┘
       │ calls submit() or click()
       ▼
┌───────────────┐
│ Browser Form  │
│ submission    │
└──────┬────────┘
       │ triggers submit event
       ▼
┌───────────────┐
│ JavaScript    │
│ event handlers│
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ HTTP Request  │
│ sent to server│
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does calling submit() always trigger JavaScript validation on the form? Commit to yes or no.
Common Belief:Calling submit() on a form element always triggers all JavaScript validations and events tied to the submit button.
Tap to reveal reality
Reality:submit() triggers the form submission event but may bypass JavaScript event handlers attached specifically to the submit button's click event.
Why it matters:Tests may pass incorrectly if validations are skipped, missing bugs that users would encounter.
Quick: Is clicking the submit button always necessary to submit a form? Commit to yes or no.
Common Belief:You must always click the submit button to submit a form in Selenium.
Tap to reveal reality
Reality:You can submit a form by calling submit() on any element inside the form, not just by clicking the button.
Why it matters:Knowing this allows simpler test code and handles forms without visible submit buttons.
Quick: Does form submission always reload the page immediately? Commit to yes or no.
Common Belief:Form submission always causes a full page reload instantly.
Tap to reveal reality
Reality:Many modern forms submit data asynchronously using AJAX, so the page may not reload immediately or at all.
Why it matters:Tests that do not wait properly after submission may fail or behave unpredictably.
Quick: Can you submit a form by sending Enter key to any input field? Commit to yes or no.
Common Belief:Pressing Enter in any input field always submits the form.
Tap to reveal reality
Reality:Only some forms are designed to submit on Enter key; others require clicking the submit button or calling submit().
Why it matters:Assuming Enter always submits can cause flaky tests or missed test cases.
Expert Zone
1
Some forms use hidden submit buttons or rely on JavaScript to enable the submit button only after validation, requiring careful element interaction.
2
Calling submit() bypasses click event handlers on the submit button, so tests may miss client-side logic if not clicking the button.
3
Forms with multiple submit buttons require selecting the correct one to trigger the intended server action.
When NOT to use
Avoid using submit() when the form relies heavily on JavaScript events attached to the submit button; instead, use click() on the button. For single-page applications with complex asynchronous submissions, consider using API-level tests or browser automation frameworks that handle JavaScript more deeply.
Production Patterns
In real-world tests, teams often prefer clicking the submit button to mimic user behavior and trigger all client-side scripts. They combine this with explicit waits for success messages or page changes. For forms without visible buttons, sending Enter keys or executing JavaScript submit() calls are fallback strategies.
Connections
Event-driven programming
Form submission triggers events that JavaScript listens to and reacts upon.
Understanding event-driven programming helps testers know why clicking vs submit() matters for triggering client-side logic.
HTTP Protocol
Form submission sends data over HTTP using GET or POST methods.
Knowing HTTP basics clarifies what happens after form submission and how data reaches the server.
Human-computer interaction (HCI)
Form submission is a key user interaction pattern on websites.
Understanding HCI principles helps design tests that mimic real user behavior and catch usability issues.
Common Pitfalls
#1Using submit() on an input element when the form requires JavaScript validation on button click.
Wrong approach:driver.findElement(By.name("email")).submit();
Correct approach:driver.findElement(By.cssSelector("button[type='submit']")).click();
Root cause:Misunderstanding that submit() bypasses click event handlers attached to the submit button.
#2Not waiting for the page or AJAX response after submitting the form.
Wrong approach:driver.findElement(By.id("submitBtn")).click(); // Immediately try to find success message without wait
Correct approach:driver.findElement(By.id("submitBtn")).click(); new WebDriverWait(driver, Duration.ofSeconds(10)) .until(ExpectedConditions.visibilityOfElementLocated(By.id("successMsg")));
Root cause:Ignoring asynchronous behavior causes tests to fail due to timing issues.
#3Trying to submit a form without locating the correct submit button when multiple exist.
Wrong approach:driver.findElement(By.tagName("button")).click(); // clicks first button, may be wrong
Correct approach:driver.findElement(By.cssSelector("button[type='submit'][name='save']")).click();
Root cause:Not distinguishing between multiple submit buttons leads to triggering wrong form actions.
Key Takeaways
Submitting forms automates sending user data to servers by triggering form submission events or clicking submit buttons.
Using submit() on form elements triggers submission but may skip JavaScript tied to button clicks, so clicking is often safer.
Properly locating form elements and submit buttons is essential for reliable automation.
Waiting for page loads or AJAX responses after submission prevents flaky tests caused by timing issues.
Advanced techniques like sending Enter keys or executing JavaScript help submit forms without visible buttons.