我正在尝试构建一个Python脚本,该脚本读取Excel文件并将数据存储在字典中。我已经构建了所有内容,但是当运行脚本时,网页会打开到正确的页面,并且不会继续前进。当我逐行运行时,找到了我所有的元素,并将代码写入了字段。
我的excel列是:名字,姓氏,电子邮件1,员工ID
我编写的python脚本是:
#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()
当我运行此模块时,网页将打开,并且不会在其中移动。我的failed.txt文件显示了Employee ID的5个结果,这意味着它在任何情况下均未成功。关于为什么此脚本无法从头到尾完成的任何想法?
我添加了堆栈跟踪并解决了超出范围错误的列表索引。现在,这就是powershell给我的。它为我提供了这4个生成器对象,然后关闭了网页,就好像它完成了一样。
<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>
我正在从字典中打印出正确的信息:
defaultdict(<class 'list'>, {'Xxxxxx': [('John', 'Doe', 'john.doe@email.com')]
为什么脚本不接收这些信息并写入我不知道的浏览器。
答案 0 :(得分:1)
您的代码缩进似乎被弄乱了,我建议修复此问题,因为缩进在python中很重要。
但是,看起来您在实际输入登录名和密码信息之前从driver
返回了sign_in
。当您尝试调用load_emp_data
时,这将导致驱动程序处于错误状态,从而导致驱动程序失败。
答案 1 :(得分:0)
编辑3- 您没有将正确的值传递给函数 函数定义为
load_emp_data(FirstName, LastName, Email1, EmployeeID, driver)
在最后一个for循环中调用
load_emp_data(excel_data[EmployeeID][0], excel_data[EmployeeID][1], excel_data[EmployeeID][2], EmployeeID, driver)
原始回复 您的程序没有执行到此为止-在sign_in函数内部
return driver
下面的代码试图获取所有元素。它甚至没有执行,因为函数到此为止。您可以尝试将return
语句下移到sign_in
函数的末尾。这将打开浏览器。
编辑-另外,在捕获异常时,您应始终尝试保留错误堆栈跟踪,将其完全覆盖会使程序难以为其他人调试。
编辑2-删除了创建驱动程序类的建议
答案 2 :(得分:0)
我已经解决了此代码的所有问题。过程很棒!
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()