How can I read the data from Excel in selenium?

238    Asked by Diyatomar in QA Testing , Asked on May 6, 2024

I am asked for a particular task that is related to automating a web-based application by using the selenium and I need to read the test data from an Excel file for use in my test scripts. How can I approach reading the data from Excel and integrating it into my Selenium test automation framework? 

Answered by Connor Peake

In the context of Selenium, you can read the data from an Excel file in Selenium by using a library like Apache POI( for Java programming language) or even openpyxl (for Python programming language). Here is how you can do so:-

Java with Apache POI

Import org.apache.poi.ss.usermodel.*;
Import org.apache.poi.xssf.usermodel.XSSFWorkbook;
Import java.io.FileInputStream;
Import java.io.IOException;
Public class ExcelReader {
    Public static void main(String[] args) throws IOException {
        // Specify the path to your Excel file
        String excelFilePath = “path/to/your/excel/file.xlsx”;
        // Create a FileInputStream to read the Excel file
        FileInputStream inputStream = new FileInputStream(excelFilePath);
        // Create a Workbook object for the Excel file
        Workbook workbook = new XSSFWorkbook(inputStream);
        // Get the desired sheet from the workbook
        Sheet sheet = workbook.getSheet(“Sheet1”); // Replace “Sheet1” with your sheet name
        // Iterate over rows in the sheet and read data
        For (Row row : sheet) {
            // Assuming the data you want is in the first column (index 0)
            Cell cell = row.getCell(0);
            If (cell != null) {
                String testData = cell.getStringCellValue();
                System.out.println(“Test Data: “ + testData);
            }
        }
        // Close the workbook and input stream
        Workbook.close();
        inputStream.close();
    }
}

Python with openpyxl

  From openpyxl import load_workbook

# Specify the path to your Excel file

  Excel_file_path = ‘path/to/your/excel/file.xlsx’

# Load the Excel workbook

  Workbook = load_workbook(filename=excel_file_path)

# Get the desired sheet from the workbook

  Sheet = workbook[‘Sheet1’]  # Replace “Sheet1” with your sheet name

# Iterate over rows in the sheet and read data

  For row in sheet.iter_rows(values_only=True):

    # Assuming the data you want is in the first column (index 0)

    Test_data = row[0]
    Print(“Test Data:”, test_data)
# Close the workbook
Workbook.close()

Here is also the example given in HTML which would Demonstrate a simple login form:-




    <meta</span> charset=”UTF-8”>

    <meta</span> name=”viewport” content=”width=device-width, initial-scale=1.0”>

    Registration Form

   



   

        Registration Form

       


           

                Username:

               

           

           

                Email:

               

           

           

                Password:

               

           

           

               

           

       


   





Your Answer

Interviews

Parent Categories