1

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', '[email protected]')]

Why the script is not taking this information and writing into the browser I do not know.

3 Answers 3

1

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 from sign_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 call load_emp_data, which would then cause it to fail.

Sign up to request clarification or add additional context in comments.

4 Comments

the indentation may have gotten messed up when I inputted into stack overflow. The indentation is fine in my editor. However, I don't think the return driver is messing it up. I use same functionality and same order in another script that works.
If you fix the indentation in stack overflow, then I can run your code and see what the mistake is.
Okay, I moved return driver down to the end of the sign_in(). The code executes until it is supposed to input the data from the excel sheet
i edited the code with the correct indentation. @Izaak Weiss
0

EDIT 3 - You are not passing the right values to your function Function defintion is

load_emp_data(FirstName, LastName, Email1, EmployeeID, driver)

Call this in your last for loop

load_emp_data(excel_data[EmployeeID][0], excel_data[EmployeeID][1], excel_data[EmployeeID][2], EmployeeID, driver)

Original reply Your program is not executing beyond this point - inside sign_in function

return driver 

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 the sign_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

15 Comments

Okay, I moved return driver down to the end of the sign_in(). The code executes until it is supposed to input the data from the excel sheet
The code does return driver from sign_in, and then passes driver into load_emp_data. There is no need to create a Webdriver class.
so what does your myfailed.txt look like now and does it go all the way to saveButton?
it lists the employee IDs that it failed on. That functionality is correct because when this script runs through my excel sheet, some rows of people may not create due to web errors or data errors and I want those IDs listed so I can troubleshoot on those specific people
so the code is working as you wanted to, right? what are you expecting now to happen?
|
0

I have resolved all issues of this code. Process works great!

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from collections import defaultdict
import openpyxl
import time
import os
import sys
import traceback

def get_excel():
    d = defaultdict(list)

    workbook = openpyxl.load_workbook(sys.argv[1])
    sheet = workbook.get_sheet_by_name('Sheet1')

    row_count = sheet.max_row
    for r in range(2, row_count + 1):
        d[(sheet.cell(r, 1).value), (sheet.cell(r, 2).value), (sheet.cell(r, 3).value), (sheet.cell(r, 4).value)]
    return d

def get_emp_data(FirstName, LastName, Email1, EmployeeID, driver):
    emp_type = driver.find_element_by_xpath("//select[@id='name_of_id']/option[text()='option_name']").click()
    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')

    f_name.send_keys(FirstName)
    l_name.send_keys(LastName)
    email.send_keys(Email1)
    employeeID.send_keys(EmployeeID)

    save = driver.find_element_by_id('saveButton').click()

def log_in():
    chrome_path = ("C:\chromedriver.exe")

    driver = webdriver.Chrome(chrome_path)
    driver.get("link to webpage")

    username = driver.find_element_by_name('login')
    password = driver.find_element_by_name('password')

    username.send_keys("username")
    password.send_keys("password")

    driver.find_element_by_id('submitButton').click()

    time.sleep(5)
    driver.find_element_by_id('newCustomerButton').click()

    return driver

def main():
    excel_data = get_excel()
    driver = log_in()
    time.sleep(3)
    for EmployeeID in excel_data:
        try:
            get_emp_data(EmployeeID[0], EmployeeID[1], EmployeeID[2], EmployeeID[3], driver)
            time.sleep(3)
            driver.get("link to webpage")
            time.sleep(5)
            driver.find_element_by_id('newCustomerButton').click()
            time.sleep(2)
            continue
        except:
            driver.quit()
    driver.get("link to webpage")

main()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.