Data Driven Testing Code for Selenium Python

September 23, 2023
data driven testing code for selenium python

Data-driven testing is a software testing methodology where test data is separated from test scripts. This allows you to perform the same set of actions on multiple sets of data, making your tests more reusable and easier to maintain. In Selenium with Python, you can implement data-driven testing using libraries like ‘unittest’ or ‘pytest’ along with data sources like CSV, Excel, or databases.

Data-driven testing in Selenium Python using data from an Excel sheet can be achieved using libraries like openpyxl. Below is a simple example demonstrating how you can perform data-driven testing for customer login functionality using multiple input values from an Excel sheet:

pip install openpyxl

import openpyxl
from selenium import webdriver
from import By

Load Excel file

excel_path = ‘path/to/your/excel_file.xlsx’
workbook = openpyxl.load_workbook(excel_path)
sheet =

Initialize WebDriver

driver = webdriver.Chrome(‘path/to/chromedriver’)

Function to perform login

def perform_login(username, password):

# Fill in login form
driver.find_element(By.ID, 'username').send_keys(username)
driver.find_element(By.ID, 'password').send_keys(password)
driver.find_element(By.ID, 'login-button').click()

Iterate through rows in the Excel sheet and perform login

for row in sheet.iter_rows(min_row=2): # Assuming data starts from the second row
username = row[0].value
password = row[1].value

# Perform login using the data from the Excel sheet
perform_login(username, password)

# Add verification steps/assertions for successful login here
    # Optionally, capture screenshots or perform other actions after login

Close the WebDriver


You can customize this script to match the structure of your application and the specifics of your test data. This is just a basic example to demonstrate the concept of data-driven testing in Selenium with Python.

If you are interested in learning Selenium Python course, then look no further than QA Training Hub. The course is instructed by an experienced trainer and real time industry expert Mr.Subba Raju. Contact QA Training Hub and become an expert in Selenium and Python.

Leave a Comment