Last Updated : 28 Aug, 2024
Comments
Improve
When working with Selenium WebDriver for web automation, you often need to interact with web elements like forms and buttons. Two commonly used methods for triggering actions in Selenium are submit()
and click()
. Understanding the differences between submit() and click() is crucial for effective test automation. The submit()
method is typically used to submit forms, while click()
simulates a mouse click on various clickable elements.
In this article, we’ll explore the key distinctions between these methods, helping you choose the right one for your testing scenarios
Table of Content
- What is submit() in Selenium WebDriver?
- What is click() in Selenium WebDriver?
- Key Differences Between submit() and click()
- Examples of Selenium Webdriver submit() and click().
- Conclusion
- Frequently Asked Questions on Selenium Webdriver submit() vs click().
What is submit() in Selenium WebDriver?
The submit() method in Selenium WebDriver is used to submit a form. It is typically invoked on a form element and triggers the form submission, which generally leads to navigation to a new page or a response from the server.
How submit() Works:
- It submits the form associated with the web element (typically an <input type=”submit”> or <button type=”submit”>).
- It is equivalent to pressing the “Submit” button in a form.
Example:
// Locate the form element and submit it
WebElement form = driver.findElement(By.id("loginForm"));
form.submit();
In the example above, the form with the ID loginForm is submitted, triggering the form’s action.
What is click() in Selenium WebDriver?
The click() method in Selenium WebDriver is used to simulate a mouse click on a web element. This method is versatile and can be used to interact with various elements, including buttons, links, and other clickable elements.
How click() Works:
- It triggers the click event on the element, which may lead to form submission, navigation, or any other action defined by the element’s event handler.
Example:
// Locate the submit button and click it
WebElement submitButton = driver.findElement(By.id("submitButton"));
submitButton.click();
In this example, the button with the ID submitButton is clicked, which might submit the form or perform other actions.
Key Differences Between submit() and click()
Here are the Key Differences Between submit() and click():
Aspect | submit() Method | click() Method |
---|---|---|
Primary Use | Used to submit forms in which the element is located. | Used to simulate a mouse click on any clickable element. |
Element Type | Typically used on form elements such as input fields (e.g., <input> , <textarea> ) within a form. | Can be used on a variety of elements like buttons, links, checkboxes, etc. |
Form Submission | Automatically submits the form associated with the element. | Does not necessarily submit a form unless the clicked element is a submit button. |
Triggering Events | Can trigger the form’s onSubmit event handler. | Triggers the onClick event handler of the element. |
Form Association | Requires the element to be inside a form. | Does not require the element to be inside a form. |
Action Simulated | Simulates pressing the Enter key on a form field. | Simulates a mouse click action. |
Common Usage | Commonly used when the form needs to be submitted without explicitly clicking a submit button. | Commonly used to click buttons, links, or checkboxes. |
Form Context | Automatically submits the parent form of the element. | Clicks the specific element, with no effect on form submission unless it is a submit button. |
Example Usage | searchBox.submit(); | searchButton.click(); |
Error Handling | May throw an error if the element is not inside a form. | Typically safer, with broader applicability across various elements. |
Examples of Selenium Webdriver submit() and click()
Using submit()
package com.example.tests;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import io.github.bonigarcia.wdm.WebDriverManager;public class SubmitExample { public static void main(String[] args) { WebDriverManager.chromedriver().setup(); // Initialize ChromeDriver instance WebDriver driver = new ChromeDriver(); // Maximize the browser window driver.manage().window().maximize(); try { // Navigate to Google driver.get("https://www.google.com"); // Locate the Google search box WebElement searchBox = driver.findElement(By.name("q")); // Type a query in the search box searchBox.sendKeys("Selenium WebDriver submit() example"); // Use submit() method to submit the form searchBox.submit(); // Pause to observe the results Thread.sleep(3000); // Sleep for 3 seconds System.out.println("Search completed using submit()."); } catch (Exception e) { System.out.println("An error occurred: " + e.getMessage()); } finally { driver.quit(); } }}
Output:
Output of submit button
Using click()
package com.example.tests;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import io.github.bonigarcia.wdm.WebDriverManager;public class ClickExample { public static void main(String[] args) { WebDriverManager.chromedriver().setup(); // Initialize ChromeDriver instance WebDriver driver = new ChromeDriver(); // Maximize the browser window driver.manage().window().maximize(); try { // Navigate to Google driver.get("https://www.google.com"); // Locate the Google search box WebElement searchBox = driver.findElement(By.name("q")); // Type a query in the search box searchBox.sendKeys("Selenium WebDriver click() example"); // Locate the Google Search button WebElement searchButton = driver.findElement(By.name("btnK")); // Pause briefly to ensure the search button is interactable Thread.sleep(1000); // Sleep for 1 second // Use click() method to submit the search searchButton.click(); // Pause to observe the results Thread.sleep(3000); // Sleep for 3 seconds System.out.println("Search completed using click()."); } catch (Exception e) { System.out.println("An error occurred: " + e.getMessage()); } finally { driver.quit(); } }}
Output:
click function output
Conclusion
In summary, both submit() and click() methods are essential tools in Selenium WebDriver for interacting with web elements. Submit() is specifically designed for submitting forms, making it ideal when dealing with input fields within a form context. On the other hand, click() offers more versatility, allowing you to simulate mouse clicks on a wide range of elements, including buttons and links.
By understanding when to use submit() vs click() in Selenium WebDriver, you can ensure more accurate and efficient automation scripts, enhancing your overall testing strategy.
Frequently Asked Questions on Selenium Webdriver submit() vs click().
Can click() also trigger form submission?
Yes, if the clicked element is a submit button, it will trigger form submission.
Is submit() always preferable over click() for form submission?
Not necessarily. Use submit() for straightforward form submissions, but click() may be needed if you are interacting with elements other than form submission buttons.
What if a form has multiple submit buttons?
If you need to test different submit buttons, you can use click() on the specific button. If the form’s default submit button is sufficient, use submit().
Previous Article
Selenium Basic Terminology
Next Article
sendKeys() not working in Selenium Webdriver