I am trying to build a python script that reads an excel file and stores the data in a dictionary. I have everything built out, but when my script is run the webpage opens to the correct page and does not move any further. All of my elements are found and the code is written into the fields when i run line by line.
My excel columns are: FirstName, LastName, Email1, EmployeeID
My python script that I have written is:
#Importing necessary tools
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from collections import defaultdict
import openpyxl
import time
import os
import sys
#loading the data from the excel
def read_excel():
d = defaultdict(list)
workbook = openpyxl.load_workbook("path of the excel file")
sheet = workbook.get_sheet_by_name('Sheet1')
row_count = sheet.max_row
for r in range(2, row_count + 1):
d[str(sheet.cell(r, 4).value)].append((str(sheet.cell(r,
1).value), str(sheet.cell(r, 2).value), str(sheet.cell(r,
3).value)))
return d
#Load all employees information from excel sheet
def load_emp_data(FirstName, LastName, Email1, EmployeeID, driver):
f_name = driver.find_element_by_name('f_name')
l_name = driver.find_element_by_name('l_name')
email = driver.find_element_by_name('contact_id.email')
employeeID = driver.find_element_by_name('contact_id.custom')
#writing in the fields
f_name.send_keys(FirstName)
l_name.send_keys(LastName)
email.send_keys(Email1)
employeeID.send_keys(EmployeeID)
#clicking save button
save = driver.find_element_by_id('saveButton').click()
def mark_iteration():
file = open('iterations.txt', 'r+')
num = file.read()
file.seek(0)
file.write(str(int(num) + 1))
file.truncate()
file.close()
def mark_failed(EmployeeID):
file = open('failed.txt', 'a')
file.write(EmployeeID + '\n')
file.close()
#sign into Lightspeed Customer Page
def sign_in():
#get to the Lightspeed customer page
chrome_path = ('C:\\chromedriver.exe')
driver = webdriver.Chrome(chrome_path)
driver.get("link to my webpage")
#input email and password
username = driver.find_element_by_name('login')
password = driver.find_element_by_name('password')
username.send_keys("login")
password.send_keys("password")
#click submit
driver.find_element_by_id('submitButton').click()
#click New Customer Button
driver.find_element_by_id('newCustomerButton').click()
return driver
def main():
excel_data = read_excel()
driver = sign_in()
time.sleep(10)
for EmployeeID in excel_data:
try:
load_emp_data(EmployeeID, excel_data[EmployeeID][0], driver)
mark_iteration()
except:
mark_failed(EmployeeID)
continue
main()
When i run this module, the webpage opens and does not move along through there. My failed.txt file shows the 5 results of Employee IDs meaning it did not succeed on any. Any ideas as to why this script is not completing from start to finish?
I have added a stack trace and solved a list index out of range error. Now This is what powershell is giving me. It gives me these 4 generator objects and then closes out the webpage as if it is finished.
<generator object main.<locals>.<genexpr> at 0x04C78530>
<generator object main.<locals>.<genexpr> at 0x04C78530>
<generator object main.<locals>.<genexpr> at 0x04C78530>
<generator object main.<locals>.<genexpr> at 0x04C78530>
I am getting the correct information printed out from my dictionary:
defaultdict(<class 'list'>, {'Xxxxxx': [('John', 'Doe', 'john.doe@email.com')]
Why the script is not taking this information and writing into the browser I do not know.
I have resolved all issues of this code. Process works great!
EDIT 3 - You are not passing the right values to your function Function defintion is
Call this in your last for loop
Original reply Your program is not executing beyond this point - inside sign_in function
The code below that is trying to get all the elements. It is not even executing because the function ends there. You can try moving down the
return
statement to the end of thesign_in
function. This will open the browser.EDIT - Also when catching an exception you should always try to keep the error stack trace, overriding it completely makes the program difficult to debug for others.
EDIT 2 - removed creating driver class suggestion
The indentation of your code seems to be messed up, and I would suggest fixing that, as indentation is significant in python.
However, it looks like you return
driver
fromsign_in
before you actually input the login and password information. This would cause the driver to be in the wrong state when you try to callload_emp_data
, which would then cause it to fail.