Bird
Raised Fist0
Selenium Javatesting~20 mins

Submitting forms in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Form Submission Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium Java code snippet?
Consider the following Selenium Java code that attempts to submit a form. What will be the result of executing this code?
Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/login");
WebElement form = driver.findElement(By.id("loginForm"));
form.submit();
System.out.println("Form submitted");
AForm submitted (form is submitted successfully)
BNoSuchElementException at findElement(By.id("loginForm"))
CTimeoutException waiting for page load after submit
DElementNotInteractableException at form.submit()
Attempts:
2 left
💡 Hint
The form element is located by its id and submit() is called directly on it.
❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies form submission success?
After submitting a form, you want to verify that the page URL contains "success". Which assertion is correct in JUnit 5?
Selenium Java
String currentUrl = driver.getCurrentUrl();
AassertFalse(currentUrl.contains("success"));
BassertEquals(currentUrl, "success");
CassertTrue(currentUrl.contains("success"));
DassertNull(currentUrl);
Attempts:
2 left
💡 Hint
Check if the URL includes the word 'success' anywhere.
❓ locator
advanced
1:30remaining
Which locator is best to find the submit button inside a form with id 'signupForm'?
You want to locate the submit button inside the form with id 'signupForm'. Which locator is the most precise and reliable?
ABy.xpath("//button[@type='submit']")
BBy.cssSelector("#signupForm button[type='submit']")
CBy.id("submit")
DBy.className("submit-btn")
Attempts:
2 left
💡 Hint
Choose a locator that scopes the button inside the specific form.
🔧 Debug
advanced
2:00remaining
Why does this Selenium Java code fail to submit the form?
The following code throws an ElementNotInteractableException when calling submit(). What is the likely cause?
Selenium Java
WebElement input = driver.findElement(By.name("username"));
input.submit();
AThe input element is not visible on the page
BThe driver has not navigated to any page yet
CThe locator By.name("username") is incorrect
Dsubmit() called on an input element instead of the form element
Attempts:
2 left
💡 Hint
submit() should be called on the form element, not on input fields.
❓ framework
expert
2:30remaining
Which Selenium Java code snippet correctly waits for form submission to complete by waiting for URL change?
You want to submit a form and wait until the URL changes to confirm submission. Which code snippet correctly implements this using WebDriverWait?
A
String oldUrl = driver.getCurrentUrl();
form.submit();
new WebDriverWait(driver, Duration.ofSeconds(10))
  .until(ExpectedConditions.not(ExpectedConditions.urlToBe(oldUrl)));
B
form.submit();
new WebDriverWait(driver, Duration.ofSeconds(10))
  .until(ExpectedConditions.urlContains("success"));
C
form.submit();
Thread.sleep(10000);
D
String oldUrl = driver.getCurrentUrl();
form.submit();
new WebDriverWait(driver, Duration.ofSeconds(10))
  .until(ExpectedConditions.urlToBe(oldUrl));
Attempts:
2 left
💡 Hint
Wait until the URL is no longer the old URL after submission.

Practice

(1/5)
1. In Selenium Java, which method is used to submit a form on a web page?
easy
A. submit()
B. click()
C. sendKeys()
D. getText()

Solution

  1. Step 1: Understand form submission in Selenium

    The submit() method is designed specifically to submit forms in Selenium.
  2. Step 2: Differentiate from other methods

    Methods like click() simulate clicking but do not necessarily submit forms; sendKeys() inputs text; getText() retrieves text.
  3. Final Answer:

    submit() -> Option A
  4. Quick Check:

    Form submission method = submit() [OK]
Hint: Use submit() to send form data, not click() [OK]
Common Mistakes:
  • Using click() instead of submit() to send form
  • Confusing sendKeys() with form submission
  • Trying to submit without locating form element
2. Which of the following is the correct way to submit a form using Selenium WebDriver in Java?
easy
A. driver.findElement(By.id("formId")).submit();
B. driver.submit(By.id("formId"));
C. driver.findElement(By.id("formId")).click();
D. driver.findElement(By.id("formId")).sendKeys("submit");

Solution

  1. Step 1: Locate the form element correctly

    Use findElement(By.id("formId")) to get the form element by its ID.
  2. Step 2: Call submit() on the form element

    Calling submit() on the form element sends the form data.
  3. Final Answer:

    driver.findElement(By.id("formId")).submit(); -> Option A
  4. Quick Check:

    Correct syntax = driver.findElement(...).submit() [OK]
Hint: Call submit() on the form element, not on driver [OK]
Common Mistakes:
  • Trying to call submit() directly on driver
  • Using click() instead of submit() on form
  • Sending keys 'submit' instead of calling submit()
3. What will happen when the following Selenium Java code runs?
WebElement input = driver.findElement(By.name("username"));
input.sendKeys("user1");
input.submit();
medium
A. Only the input field is cleared but form is not submitted.
B. An exception is thrown because submit() cannot be called on input elements.
C. The form containing the input field is submitted with 'user1' as username.
D. The page reloads without submitting any data.

Solution

  1. Step 1: Identify element and actions

    The code finds an input field named "username", enters "user1" text, then calls submit() on that input element.
  2. Step 2: Understand submit() behavior on child elements

    Calling submit() on any child element inside a form submits the entire form containing that element.
  3. Final Answer:

    The form containing the input field is submitted with 'user1' as username. -> Option C
  4. Quick Check:

    submit() on input submits parent form [OK]
Hint: submit() on input submits its parent form [OK]
Common Mistakes:
  • Thinking submit() only works on form elements
  • Expecting submit() to clear input instead of submitting
  • Assuming submit() throws exception on input elements
4. Consider this Selenium Java code snippet:
WebElement form = driver.findElement(By.id("loginForm"));
form.click();
form.submit();

What is the main issue with this code?
medium
A. submit() must be called before click() on the form.
B. submit() cannot be called on a form element.
C. The form element is not located correctly.
D. Calling click() on the form element may cause unexpected behavior before submit.

Solution

  1. Step 1: Analyze click() on form element

    Calling click() on a form element is unusual and may trigger unexpected UI behavior or errors.
  2. Step 2: Confirm submit() usage

    Calling submit() on the form element is correct and will submit the form.
  3. Final Answer:

    Calling click() on the form element may cause unexpected behavior before submit. -> Option D
  4. Quick Check:

    click() on form is risky; submit() is correct [OK]
Hint: Avoid click() on form; use submit() directly [OK]
Common Mistakes:
  • Thinking submit() cannot be called on form
  • Assuming click() is needed before submit()
  • Ignoring potential side effects of clicking form
5. You want to submit a form that has no id or name attribute but contains a submit button with class "submit-btn". Which Selenium Java code correctly submits the form?
hard
A. driver.findElement(By.className("submit-btn")).submit();
B. driver.findElement(By.className("submit-btn")).click();
C. driver.findElement(By.tagName("form")).submit();
D. driver.findElement(By.tagName("button")).submit();

Solution

  1. Step 1: Locate the submit button by class

    Since the form lacks id or name, find the submit button using its class "submit-btn".
  2. Step 2: Call click() on the submit button element

    Calling click() on the submit button simulates pressing the button, which submits the form.
  3. Final Answer:

    driver.findElement(By.className("submit-btn")).click(); -> Option B
  4. Quick Check:

    click() on submit button triggers form submission [OK]
Hint: Submit form by clicking the submit button [OK]
Common Mistakes:
  • Using submit() instead of click() on submit button
  • Trying to submit form by tagName without verifying uniqueness
  • Calling submit() on button tag which may not be inside form
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard