How to read test data from an excel sheet and use it to test facebook login in Selenium Webdriver?


We can read test data from an excel sheet and use it to test Facebook login with Selenium webdriver in Python. We can access excel with Python with the help of the OpenPyXL library.

To install this library, we have to run the command - pip install openpyxl. Besides, we have to add the statement import openpyxl in our code. To start with, we have to load the excel workbook with the help of the load_workbook method.

The path of the workbook is passed as a parameter to this method. Then we have to determine its active sheet by applying the sheet method on the loaded workbook object.

To test the Facebook login page, we need to have the email and password of the user and this data is saved in an excel workbook as shown in the below image. The email – abc@gmail.com is at cell (address: row=2, column=1) and password – test123 is at cell(address: row=2, column=1).

To read these data, we shall use the cell method on the active sheet. The row and column numbers are passed as parameters to this method. Then the value method is applied on those particular cell addresses to read values within it.

Example

import openpyxl
from selenium import webdriver
#configure workbook path
b = openpyxl.load_workbook("C:\Data.xlsx")
#get active sheet
sht = b.active
#get cell address of email within active sheet
e = sht.cell (row = 2, column = 1)
#get cell address of password within active sheet
p = sht.cell (row = 2, column = 2)
#get values
email = e.value
passw = p.value
#set chromodriver.exe path
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
driver.implicitly_wait(0.5)
#launch URL
driver.get("https://www.facebook.com/")
#identify element
l = driver.find_element_by_id("email")
#enter email obtained from excel
l.send_keys(email)
m = driver.find_element_by_id("pass")
#enter password obtained from excel
m.send_keys(passw)
#get values entered
s = l.get_attribute("value")
t = m.get_attribute("value")
print("Email is: ")
print(s)
print("Password is: ")
print(t)
#browser quit
driver.quit()

Output

Updated on: 08-Apr-2021

851 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements