How to click button using selenium webdriver with Java?

In this post we are going to automate an Button by using click method from selenium webdriver.

Let’s use chropath to inspect submit Button and identify the WebElement locator.

As it is visible that Button doesnot have an id, hence taking the xpath of that WebElement (“//button[contains(text(),’Submit’)]”). Let’s use this ID locator to click the submit button.

Click() : Click is an predefined method from Selenium webdriver class, where we can Select the button field.

Steps to Click Button Using Selenium WebDriver with Java :

  1. Start writing code in an Java file.
  2. First let’s launch the browser (say using chrome here in the example).
  3. Enter the Appliction under test(AUT) url in the browser.


  1. Maximize the browser window.


  1. To identify the Submit button we are using By.xpath(“//button[contains(text(),’Submit’)]”) (i.e xpath locator)

Let’s discuss about this line of code :

WebElement submitButton = driver.findElement(By.xpath(“//button[contains(text(),’Submit’)]”));

–> driver : is the webdriver object.

–> findElement() : Used to identify or locate the elements to perform the operation.

–> By.xpath(“//button[contains(text(),’Submit’)]”): By.xpath helps the findElement() to find the elements with xpath locator. Here in this example we are using (xpath = “//button[contains(text(),’Submit’)]”) to identify the Submit button field.

–> click() : Webdriver command used to click Submit button based on the locator.

–> WebElement submitButton : Reference of the submitButton element is stored into submitButton variable.

  1. To click the SubmitButton use click method. By taking the reference of WebElement i.e submitButton (;

  1. Execute the script by right click run as –> Java application.
  2. Once the script is executed we can observe the submit button is clicked.
  3. Final step is to close the browser

Leave a Comment

Your email address will not be published. Required fields are marked *