read excel selenium

# How to read & write to Excel file

import XL_utility # Importing from the second file
from selenium import webdriver
import unittest
import time


class Test_Excel(unittest.TestCase):

    def setUp(self):

        self.driver = webdriver.Chrome("path for browser driver...")
        self.driver.implicitly_wait(10)
        self.driver.get("web adress...")
        self.driver.maximize_window()

        return self.driver

    def test_read_excel_file(self):

        path = "file location for your excel file..."
        print(path.title())

        rows = XL_utility.getRowCount(path, "Sheet1")

        for r in range(2, rows + 1):
            username = XL_utility.readData(path, "Sheet1", r, 1)
            password = XL_utility.readData(path, "Sheet1", r, 2)

            self.driver.find_element_by_name("username").send_keys(username)
            self.driver.find_element_by_name("password").send_keys(password)

            self.driver.find_element_by_class_name("auth-form__submit").click()

            time.sleep(10)


            if self.driver.title == "title when you're logged in":
                print("Test is passed!")
                XL_utility.writeData(path, "Sheet1", r, 3, "Test passed")
            else:
                print("Test failed")
                XL_utility.writeData(path, "Sheet1", r, 3, "Test failed")

            self.driver.back()
            return self.driver



if __name__ == "__main__":
    unittest.main()
    
    
# This following code should be placed in a new python file...


import openpyxl


def getRowCount(file, sheetName):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    return sheet.max_row


def getColumnCount(file, sheetName):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    return sheet.max_column


def readData(file, sheetName, rownum, columnno):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    return sheet.cell(row=rownum, column=columnno).value


def writeData(file, sheetName, rownum, columnno, data):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    sheet.cell(row=rownum, column=columnno).value = data
    workbook.save(file)

4.29
7

                                    I used Apache POI libraries to read and write from
excel file, I add the Apache poi dependencies to my pom.xml file.
In order to connect I use following classes

-FileInputStream from Java. It is used to create connection
to the file. We pass the file path as constructor to it.

-WorkBook is a class that represents the excel file. We create
workbook is a class from Apache POI that represents the excel
file. We create Workbook object using the FileInputStream
object.

-Sheet represents a single sheet from the excel file. We create
sheet using Workbook object. We can create worksheet using
the 0 based index.

public String readExcel(String path, String sheetName, int rowNum, int colNum){
        try {
            FileInputStream file = new FileInputStream(path);
            Workbook book = WorkbookFactory.create(file);
            Sheet sheet = book.getSheet(sheetName);
            Row row = sheet.getRow(rowNum);
	    	Cell cell = row.getCell(colNum);
            String cellData = cell.toString();
            return cellData;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

How to get row and column numbers:
    int rowCount = workSheet.getLastRowNum()+1; ==> why we add '+1'? Because 
    row num starts from 0.
    int colCount = workSheet.getRow(0).getLastCellNum();
    String sheetName = workSheet.getSheetName();

The data we get from excel can be converted to different formats such as set, 
list, map

-------------------EXCEL WRITING AND SAVING---------------------------------

The first creation part is same then go to a cell where you want to write.

Row row = sheet.getRow(0)
Cell resultCell = row.getCell(2)

Let's imagine you have values in index 0 and index 1.
Now you want to create a cell on index 2. First, check if it is null to 
avoid problems.

if(resultCell==null){
   resultCell = row.createCell(2);
}
resultCell.setValue("Germany");

in order to save:
// class is used to open file and write to it
FileOutputStream fileOutputStream = new FileOutputStream("src/test/resources/
Countries.xlsx");
// write the changes to the file and save
workbook.write(fileOutputStream);

4.29 (7 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to take data in excel read from excel read data from excel in python excel in selenium excel file in selenium how to read "" from excel how to read data from excel sheet excel read data from access automatically read data into excel read excel data in selenium excel data read read data in excel how to read data from excel file in python how to get excel data in read excel in selenium selenium python read excel get data from excel file read data from excel reading excel file in selenium load data from excel read the data from excel in selenium how to fetch data from excel sheet in selenium how to read data from excel file in java using selenium webdriver how to read the data from excel in selenium excel read write in selenium excel utility for selenium selenium, get excel data get cell data from excel in selenium One line code which can read the excel sheet in selenium using java One line code which can read the excel sheet in selenium reading and writing data to excel file using upaal selenium locate excel file path selenium java get excel file path excel parsing in selenium poi read excel sheet in selenium java How to read data from excel spreadsheet using XSSFSheet read data from excel sheet selenium code to read excel file in selenium webdriver Read data from excel document into SQL read image from excel read excel file in java apache poi selenium read data from excel get data from excel in selenium java xl file handeling read excel selenium how to read excel with selenium selenium excel how to read data from excel
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source