Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
How to Disable Submit Button on Form Submit in JavaScript ?
Next article icon

How to submit a form in java Selenium webdriver if submit button can't be identified?

Last Updated : 10 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Submitting a form in Selenium WebDriver can sometimes be challenging, especially when the submit button cannot be easily identified. In such cases, Java Selenium WebDriver offers alternative methods to interact with web elements and successfully submit the form.

Whether the submit button lacks a unique identifier or is hidden within complex HTML structures, Selenium provides robust techniques to handle these situations effectively.

  • We can submit a form in Selenium webdriver even if the submit button cannot be identified.
  • This can be achieved by locating an element within the form tag and the applying submit method on it.
  • A form in the html code is identified by the <form> tag.

Let us investigate the HTML code of the element within a form tag -

Html
Login page Elements

In the above example, we shall try to submit the form with the help of the Username or Password field and not by clicking on the Login button.

Syntax

driver.findElement(By.className("username")).sendKeys("Admin");
driver.findElement(By.className("password")).sendKeys("admin123");
driver.findElement(By.xpath("//button[@type='submit']")).submit();

Example code:

Java
package seleniumpractice;  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;  import java.util.concurrent.TimeUnit;  public class SubmitForm {  	 	   public static void main(String[] args) throws Exception { 	  //    System.setProperty("webdriver.gecko.driver", 	    //     "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe"); 	      WebDriverManager.chromedriver().setup(); 	      WebDriver driver = new ChromeDriver(); 	      //implicit wait 	      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 	      //URL launch 	      driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login"); 	      // identify element within form 	      WebElement m=driver.findElement(By.name("username")); 	      m.sendKeys("Admin"); 	      WebElement n=driver.findElement(By.name("password")); 	      n.sendKeys("admin123"); 	      //submit form 	      WebElement o=driver.findElement(By.xpath("//button[@type='submit']")); 	      o.submit(); 	      Thread.sleep(1000); 	      System.out.println("Page title: " + driver.getTitle()); 	      driver.close(); 	   } 	} 

Explanation of above code:

  • org.openqa.selenium.*: These imports are for the selenium WebDriver classes that allow you to interact with web elements.
  • io.github.bonigarcia.wdm.WebDriverManager: This is used to automatically manage the browser driver binaries.
  • java.util.concurrent.TimeUnit: This provides time related constants, used here for setting timeouts.
  • SubmitForm: This declares a public class named.
  • Main Method: This is the entry point of the java application. throws Exception allows the code to throw exception that might occur during execution, such as Interrupted Exception.
  • WebDriverManager.chromedriver().setup: This method sets up the ChromeDriver binary automatically.
  • new ChromeDriver(): This creates an instance of the ChromeDriver, which is used to control the Chrome browser.
  • This sets an implicit wait of 5 seconds for locating elements. If the element is not immediately found, WebDriver will wait up to 5 seconds before throwing a NoSuchElementException.
  • This navigates the browser to the specified URL, which is the login page of OrangeHRM.
  • driver.findElement(By.name("username")); : Finds the username input field by its name attribute.
  • m.sendKeys("Admin");:Types Admin into the username field.
  • driver.findElement(By.name("password")); :Finds the password input field by its name attribute.
  • n.sendKeys("admin123"); : Types admin123 into the password field.
  • driver.findElement(By.xpath("//button[@type='submit']")); : Finds the submit button using Xpath.
  • o.submit(); : Submits the form. Note: The submit() method should be used on form elements. For non-form elements like buttons, click() is generally preferred.
  • Thread.sleep(1000): Pauses execution for 1 second to allow the page to load.
  • driver.getTitle( ): Retrieves the title of the current page and prints it to the console.
  • driver.close(); Closes the current browser window.

Output

SubmitForm-output
SubmitForm output

Conclusion

When the submit button cannot be identified in a Java Selenium WebDriver test, using alternative methods like submitting the form through input fields or triggering the form submission via JavaScript can be effective strategies. These approaches ensure that your automated tests remain reliable and efficient, even when dealing with complex or dynamically generated web pages.

By mastering these techniques, you can enhance your ability to handle a wide range of web automation challenges in Selenium.


Next Article
How to Disable Submit Button on Form Submit in JavaScript ?

S

sunilgacd6r
Improve
Article Tags :
  • Selenium

Similar Reads

  • How to get text from the alert box in java Selenium Webdriver?
    In Automation testing, we can capture the text from the alert message in Selenium WebDriver with the help of the Alert interface. By default, the webdriver object has control over the main page, once an alert pop-up gets generated, we have to shift the WebDriver focus from the main page to the alert
    3 min read
  • How to Drag and Drop an Element using Selenium WebDriver in Java?
    Selenium is an open-source web automation tool that supports many user actions to perform in the web browser. Automating a modern web page that has a drag and drop functionality and drag and drop is used to upload the files and so many user activities. so to perform the drag and drop actions the sel
    2 min read
  • How to Disable Submit Button on Form Submit in JavaScript ?
    Forms are a crucial part of web development as they allow users to submit data to a server. However, sometimes we want to customize the behavior of HTML forms and prevent the default behavior from occurring. This allows you to create a more interactive and user-friendly web application. In this arti
    3 min read
  • How to ask the Selenium-WebDriver to wait for few seconds in Java?
    An open-source framework that is used for automating or testing web applications is known as Selenium. There are some circumstances when the particular component takes some time to load or we want a particular webpage to be opened for much more duration, in that case, we ask the Selenium web driver
    9 min read
  • How to check that the element is clickable or not in Java Selenium WebDriver?
    Ensuring that an element is clickable in your Selenium WebDriver tests is crucial for validating the functionality and user experience of your web applications. In Java Selenium WebDriver, checking if an element is clickable before interacting with it can help avoid errors and ensure that your test
    3 min read
  • Selenium WebDriver Handling Radio Buttons Using Java
    A platform-independent language that is used to build various applications is known as Java. Java can also be used to automate the web drivers. There are various automation tools available, out of which Selenium is the most common one. We can automate the opening of the website, clicking of push but
    4 min read
  • How to do session handling in Selenium Webdriver using Java?
    In Selenium WebDriver, managing browser sessions is crucial for ensuring that each test runs independently and without interference. A browser session in Selenium is identified by a unique session ID, which helps track and manage the session throughout the test. Proper session handling in Selenium W
    4 min read
  • How to click the 'Ok' button inside an alert window with a Java Selenium command?
    An alert window is a pop-up box shown over the webpage to convey error messages using the alert() function of JavaScript. It has only one button "OK" which the user must press to continue browsing the webpage. A person might automate the website testing process and stumble upon pages that display al
    3 min read
  • How to automate google Signup form in Selenium using java?
    For any QA engineer or developer, automating the Google Signup form with Selenium may be a hard nut to crack. Also, as the needs are increasing toward automated testing, in this article, we will learn how to deal with a complicated web form like Google Signup. We will show you how to automate the Go
    4 min read
  • How to Click on a Hyperlink Using Java Selenium WebDriver?
    An open-source tool that is used to automate the browser is known as Selenium. Automation reduces human effort and makes the work comparatively easier. There are numerous circumstances in which the user wants to open a new page or perform a certain action with the click of the hyperlink. In this art
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences