How to Click Submit Button in Selenium Webdriver Java

Selenium is an open-source web-based automation tool that is implemented using a web driver. We will be using geckodriver because Selenium 3 enables geckodriver as the default WebDriver implementation for Firefox.

Pre-requisites:

  1. geckodriver.exe
  2. maven dependency selenium
 <dependency>
    <groupid>org.seleniumhq.selenium</groupid>
	<artifactid>selenium-java</artifactid>  
	<version>4.1.1</version> 
 </dependency>

Steps:

It's a 4 steps process:

  1. Set webdriver.gecko.driver and its' path as a system property.
  2. Set the firefox diver and browse to the website link.
  3. Get HTML Div containing submit button as a web element using webdriver.findElement() function and CSS selector.
  4. Find submit button using CSS selector and hit click.

Let’s see all the above steps in the code. We will use webelement.click() function to hit submit.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.List;

public class ClickSubmitBtn {

    public static String GECKODRIVER_PATH = "F:\\WORK\\SeleniumShortTasks\\ClickSubmit\\src\\main\\resources\\geckodriver.exe";

    public static void main(String[] args) {
        //set firefox webdriver
        System.setProperty("webdriver.gecko.driver", GECKODRIVER_PATH);
        WebDriver driver = new FirefoxDriver();

        //get the firefox browser & Browse the Website
        String siteLink = "https://docs.google.com/forms/d/e/1FAIpQLSd3hbwlnuLuwVVnP3iEAjQwXQuZ7PuiTNMuuYe0TcHHYqZ6uQ/viewform";
        driver.get(siteLink);

        //First fill the form , a little
        //Mark 2 checkboxes
        driver.findElements(By.cssSelector("div.ulDsOb")).get(3).click();
        driver.findElements(By.cssSelector("div.ulDsOb")).get(4).click();

        //Get Submit button Div
        WebElement submitDiv = driver.findElement(By.cssSelector("div.lRwqcd"));
        //Find button and Hit click
        submitDiv.findElement(By.cssSelector("span.l4V7wb.Fxmcue")).click();


    }
 }

Output:


All resources used in this tutorial are attached:
  • source code
  • geckdriver.exe
Download

Comments