Reading an Excel File and Submitting a Web Form Using Selenium Java


  1. geckodriver.exe
  2. maven dependencies for apache poi & Selenium
   <!-- -->

This automation process is divided into two parts:
  1. Reading from an Excel file 
  2. WebForm Automated Filling & Submission

Reading an MS Excel File Using Apache POI:

Apache POI is an open-source java library developed and distributed by Apache Software Foundation to design and modify MS Office files. We will be using XSSF a class apache poi. XSSF: XML Spreadsheet Format is used for .xlsx files of MS-Excel.
  1. Reading an Excel file using Java Apache POI is a six-step process: 
  2. Put Excel file on input Stream.
  3. Create a workbook instance of an excel sheet.
  4. Get to the desired sheet of file.
  5. Iterate rows of files.
  6. Iterate over all cells in one row and place the values into an object of a custom class Record.
  7. Repeat steps 4 and 5 until all data is read
Let’s see all the above steps in the code. The following function readFile() read all the values in the Excel file – cell by cell and returns a list of Records.
public static ArrayList<Record> readFile(String filePath) throws IOException {

        ArrayList<Record> recordArrayList = new ArrayList<>();
        try {

            //put file on input stream
            FileInputStream file = new FileInputStream(new File(filePath));
            XSSFWorkbook workbook = new XSSFWorkbook(file);

            //Get desired sheet from the workbook
            XSSFSheet sheet = workbook.getSheetAt(0);

            //Iterate through each row one by one
            Iterator<Row> rowIterator = sheet.iterator();
            Row row =;

            //get value from each cell and insert it into Record object
            while (rowIterator.hasNext()) {

                row =;
                Record record = new Record();

                //insert data from cells to record Object
                Cell cell = row.getCell(0);
       = (int) cell.getNumericCellValue();

                cell = row.getCell(1);
       = cell.getStringCellValue();

                cell = row.getCell(2);
                record.age = (int) cell.getNumericCellValue();

                cell = row.getCell(3);
                record.gender = cell.getStringCellValue();

                cell = row.getCell(4);
       = cell.getStringCellValue();

                cell = row.getCell(5);
       = (cell.getStringCellValue()).split(",");

                cell = row.getCell(6);
                record.goal = cell.getStringCellValue();


        } catch (Exception e) {

        return recordArrayList;

Following is the code for class:

public class Record {
    int id;
    int age;
    String name;
    String city;
    String gender;
    String[] courses;
    String goal;

WebForm Automation Using Selenium:

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.

The process involves 3 base steps.
  1. Set webdriver.gecko.driver and its' path as a system property.
  2. Set the firefox diver and wait for attribute.
  3. //set firefox webdriver
            WebDriver driver = new FirefoxDriver();
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(120));
  4. Fire up the driver by using the hyperlink of web form.
  5. //get the firefox browser & Browse the Google form
                String wedFormHyperlink = "";
The following 6 steps will automate the process of form filling and submission:
  1. Apply and wait for the DOM to be ready.
  2. Identify HTML & CSS selectors of fields in web form.  
  3. Click the field section to make it visible for getting the corresponding web element. 
  4. Get the web element or a list of web elements by using css-selectors of the corresponding field of the form.   
  5. Send the value of the field as key (in case of input fields)
    1. In case of drop-down, checkboxes, radio buttons and etc, click the flex element. (Got as web element using css selector)
  6. Repeat steps 2-5 to fill and submit the form. 
Let’s see all the above steps in the code. The following function fillTheForm() fills the form using values in the Record object, submits the form, and then redirects to fill against other records.
public static void fillTheForm(Record rc, WebDriverWait wait, WebDriver driver) {

        //wait until document is ready
        wait.until(ExpectedConditions.titleContains("Self-Development Boot Camp"));

        //insert data fom ArrayList to the web form
        List<WebElement> genderElements = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.nWQGrd.zwllIb")));

        //set Gender
        if (rc.gender.equals("Male"))

        //set input fields (id,name,age.goals)
        List<WebElement> inputElements = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("input.whsOnd.zHQkBf")));



        WebElement textarea = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("textarea.KHxj8b.tL9Q4c")));;

        //open cities drop-drown menu
        //get dropdown items
        List<WebElement> allCities = driver.findElements(By.cssSelector("span.vRMGwf.oJeWuf"));
        //getting the valid index after removing empty divs
        int allCitiesIndex = allCities.size() - 6;

        //Removing extra divs
        for (int i = 0; i < allCitiesIndex; i++)

        //set city-items
        Boolean citiesFlag = false;
        for (WebElement wb : allCities) {

            if ( {

                citiesFlag = true;
        if (citiesFlag == false)

        //click the courses section to make it available for selenium processing

        //Set Courses
        List<WebElement> allCourses = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.ulDsOb")));
        //remove extra divs

        //set course-checkbox
        for (WebElement course : allCourses) {

            for (String eachCourse : {
                if (eachCourse.equals(course.getText()))

        WebElement submitDiv = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.lRwqcd")));
        //submit button click

        //move to the next response - redirect the webdriver to the same form
        WebElement nextResponseForm = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.c2gzEf")));



All resources used in this tutorial are attached:
  • source code
  • geckdriver.exe
  • Excel File
  • Jar files to be included in the classpath