如何遍历多组数据并将数据重复输入到一个方法中? (tkinter,硒,python)
How to iterate through multiple sets of data and input data into a method repeatedly? (tkinter, selenium, python)
我正在尝试编写一个自动化程序,它将从单个 UI 中获取用户输入的数据,并将该数据自动输入到单个网站或多个网站中。
当按下 "Get Quote" 按钮时,chrome 浏览器会话打开并将 UI 中的所有数据放入网站内相应的输入字段。
我现在想弄清楚的是当存在多于 1 组数据时如何考虑这些情况。例如:
这是我希望程序能够执行的操作的图片。基本上输入第二组数据,然后点击"Add line Item",如果没有数据可以输入,那么脚本会自动点击"Generate Smart Quote."
能够输入第二或第三组数据的最佳方法是什么?必须以相同的方法重新输入数据。这是上下文的一些代码。
from tkinter import *
from tkinter import font
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
orders = {}
def order_data():
# start by adding a dict to the dict with key of length dict,
# the first key will be 0, then 1, 2 and so on
orders[len(orders)] = {}
# next you can start adding data using len - 1 to access the correct key
# and so on for all your data
orders[len(orders) - 1]['handling unit'] = e3.get()
orders[len(orders) - 1]['pieces'] = e4.get()
orders[len(orders) - 1]['description'] = e5.get()
orders[len(orders) - 1]['length'] = e6.get()
orders[len(orders) - 1]['width'] = e7.get()
orders[len(orders) - 1]['height'] = e8.get()
orders[len(orders) - 1]['weight'] = e9.get()
orders[len(orders) - 1]['classification'] = e10.get()
def GetQuote():
origin_zip = int(e1.get()) #gets entries stores data here
destination_zip = int(e2.get())
handling_unit = [e3.get()]
pieces = [e4.get()]
description = [e5.get()]
length = [e6.get()]
width = [e7.get()]
height = [e8.get()]
weight = [e9.get()]
classification_list =[e10.get]
driver = webdriver.Chrome("/home/***/***/chromedriver")
driver.maximize_window() #driver.set_window_size(10, 10)
#1st Tab (shipco.com)
driver.get("https://tms.shipco.com/Main/Home")
driver.implicitly_wait(1)
#1st Tab Login Page
driver.find_element(By.XPATH, "//button[@id='dropdownMenuButton']").click()
driver.find_element(By.XPATH, "//input[@id='UserName']").send_keys('***')
driver.find_element(By.XPATH, "//input[@id='Password']").send_keys('***')
driver.find_element(By.XPATH, "//button[@class='btn btn-primary btn-block']").click()
#1st Tab 2nd Page
driver.find_element(By.XPATH, "//input[@id='OriginZip']").send_keys(origin_zip)
driver.find_element(By.XPATH, "//input[@id='DestinationZip']").send_keys(destination_zip)
driver.find_element(By.XPATH, "//button[@class='btn btn-secondary form-control']").click()
#Will have to add a conditional statement here to iterate through instances of multiple items/dimensions/weights
#1st Tab 3rd Page
driver.refresh();
for i in range(len(orders)):
driver.find_element(By.ID, "handlingunits").send_keys(orders[i]['handling unit'])
driver.find_element(By.ID, "quantityofgoods").send_keys(orders[i]['pieces'])
driver.find_element(By.ID, "commoditydescription").send_keys(orders[i]['description'])
driver.find_element(By.ID, "sizelength").send_keys(orders[i]['length'])
driver.find_element(By.ID, "sizewidth").send_keys(orders[i]['width'])
driver.find_element(By.ID, "sizeheight").send_keys(orders[i]['height'])
driver.find_element(By.ID, "totalweight").send_keys(orders[i]['weight'])
driver.implicitly_wait(10)
driver.find_element(By.XPATH, "//a[@class='ng-binding']").click() #this will click the object that gets class for shipment
if i < len(orders):
driver.find_element(By.XPATH, "//button[@class='btn btn-info']").click()
driver.find_element(By.XPATH, "//span[contains(text(),'Generate Smart Quote')]").click()
def insert_into_textbox(): #generates input fields for the next set of items
order_data()
#get the inputs
handling_unit = e3.get()
pieces = e4.get()
description = e5.get()
length = e6.get()
width = e7.get()
height = e8.get()
weight = e9.get()
classification = e10.get()
textbox_display = (handling_unit.ljust(11)+pieces.ljust(13)+description.ljust(11)+length.ljust(10)+width.ljust(11)+height.ljust(11)+weight.ljust(10)+classification+"\n")
textbox.insert("end",textbox_display)
e3.delete(0, "end")
e4.delete(0, "end")
e5.delete(0, "end")
e6.delete(0, "end")
e7.delete(0, "end")
e8.delete(0, "end")
e9.delete(0, "end")
e10.delete(0, "end")
master = Tk()
master.title("Quote Automator")
master.configure(background="#eef56e")
arial8 = font.Font(family="Arial", size=8, weight=font.BOLD)
algerian8 = font.Font(family="Algerian", size=8, weight=font.BOLD)
e1 = Entry(master, borderwidth=5, width=12) #origin zip
e2 = Entry(master, borderwidth=5, width=12) #destination zip
e3 = Entry(master, borderwidth=5, width=12) #handling unit(s)
e4 = Entry(master, borderwidth=5, width=12) #piece(s)
e5 = Entry(master, borderwidth=5, width=13) #description(s)
e6 = Entry(master, borderwidth=5, width=12) #length(s)
e7 = Entry(master, borderwidth=5, width=12) #width(s)
e8 = Entry(master, borderwidth=5, width=12) #height(s)
e9 = Entry(master, borderwidth=5, width=12) #weight(s)
e10 = Entry(master, borderwidth=5, width=12) #class(s)
# grid method customizes position
e1.grid(row = 0, column = 2, pady = 1, sticky="W") #origin zip
e2.grid(row = 0, column = 4, pady = 1, sticky="W") #destination zip
e3.grid(row = 3, column = 1, pady = 1, sticky="W") #handling unit(s)
e4.grid(row = 3, column = 2, pady = 1, sticky="W") #piece(s)
e5.grid(row = 3, column = 3, pady = 1, sticky="W") #description(s)
e6.grid(row = 3, column = 4, pady = 1, sticky="W") #length(s)
e7.grid(row = 3, column = 5, pady = 1, sticky="W") #width(s)
e8.grid(row = 3, column = 6, pady = 1, sticky="W") #height(s)
e9.grid(row = 3, column = 7, pady = 1, sticky="W") #weight(s)
e10.grid(row = 3, column = 8, pady = 1, sticky="W") #class(s)
textbox=Text(master, width=9, borderwidth=5, height=7)
textbox.grid(row=5, column=1, columnspan=9, sticky="nsew")
scrollbar=Scrollbar(master, orient="vertical", command=textbox.yview)
scrollbar.grid(row=5, column=10, sticky=NS)
textbox.configure(yscrollcommand=scrollbar.set)
b0 = Button(master, text = "Add Set", bg="white", fg="black", font=arial8, command=insert_into_textbox) #Add Next Set Button
b1 = Button(master, text = "Caculate Class", bg="yellow", font=arial8, command=set_class)
b2 = Button(master, text = "Get Quote", bg="#6fff00", fg="#7170ff", font=arial8, width=10, command=GetQuote)
b0.grid(row = 3, column = 0, sticky = W) # Add Next Set Button Positioning
b1.grid(row = 0, column = 8, sticky = E) # Calculate Class Button Positioning
b2.grid(row = 0, column = 7, sticky = E) # Get Quote Button Positioning
mainloop()
您可以将每一行添加到字典中,每个键都是一个数字。然后每个值可以是另一个字典或命名元组。然后您可以循环遍历方法中的数字键以获取每一行的值。
例如,您可以使用字典创建一个函数来接收数据。
orders = {}
def order_data():
# start by adding a dict to the dict with key of length dict,
# the first key will be 0, then 1, 2 and so on
orders[len(orders)] = {}
# next you can start adding data using len - 1 to access the correct key
orders[len(orders) - 1]['handling unit'] = e3.get()
# and so on for all your data
然后当您调用您的方法时,您可以使用 for 循环为每一行输入数据。
for i in range(len(orders)):
class.method(i)
然后在每个 send_keys 函数的方法中,您可以调用字典
driver.find_element(By.ID, "handlingunits").send_keys(orders[i]['handling unit'])
如果您愿意,也可以在方法内执行 for 循环。
for i in range(len(orders)):
driver.find_element(By.ID, "handlingunits").send_keys(orders[i]['handling unit'])
driver.find_element(By.ID, "quantityofgoods").send_keys(orders[i]['pieces'])
driver.find_element(By.ID, "commoditydescription").send_keys(orders[i]['description'])
driver.find_element(By.ID, "sizelength").send_keys(orders[i]['length'])
driver.find_element(By.ID, "sizewidth").send_keys(orders[i]['width'])
driver.find_element(By.ID, "sizeheight").send_keys(orders[i]['height'])
driver.find_element(By.ID, "totalweight").send_keys(orders[i]['weight'])
driver.implicitly_wait(10)
driver.find_element(By.XPATH, "//a[@class='ng-binding']").click() #this will click the object that gets class for shipment
if i < len(orders):
driver.find_element(By.XPATH, "//button[@class='btn btn-info']").click() #add another line of items if avaiable
driver.find_element(By.XPATH, "//span[contains(text(),'Generate Smart Quote')]").click()
将 'add new line' 部分放入循环中,这样的逻辑将使程序继续添加,直到数据用完,然后它会单击 'Generate Smart Quote' 按钮。
我正在尝试编写一个自动化程序,它将从单个 UI 中获取用户输入的数据,并将该数据自动输入到单个网站或多个网站中。
当按下 "Get Quote" 按钮时,chrome 浏览器会话打开并将 UI 中的所有数据放入网站内相应的输入字段。
我现在想弄清楚的是当存在多于 1 组数据时如何考虑这些情况。例如:
这是我希望程序能够执行的操作的图片。基本上输入第二组数据,然后点击"Add line Item",如果没有数据可以输入,那么脚本会自动点击"Generate Smart Quote."
能够输入第二或第三组数据的最佳方法是什么?必须以相同的方法重新输入数据。这是上下文的一些代码。
from tkinter import *
from tkinter import font
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
orders = {}
def order_data():
# start by adding a dict to the dict with key of length dict,
# the first key will be 0, then 1, 2 and so on
orders[len(orders)] = {}
# next you can start adding data using len - 1 to access the correct key
# and so on for all your data
orders[len(orders) - 1]['handling unit'] = e3.get()
orders[len(orders) - 1]['pieces'] = e4.get()
orders[len(orders) - 1]['description'] = e5.get()
orders[len(orders) - 1]['length'] = e6.get()
orders[len(orders) - 1]['width'] = e7.get()
orders[len(orders) - 1]['height'] = e8.get()
orders[len(orders) - 1]['weight'] = e9.get()
orders[len(orders) - 1]['classification'] = e10.get()
def GetQuote():
origin_zip = int(e1.get()) #gets entries stores data here
destination_zip = int(e2.get())
handling_unit = [e3.get()]
pieces = [e4.get()]
description = [e5.get()]
length = [e6.get()]
width = [e7.get()]
height = [e8.get()]
weight = [e9.get()]
classification_list =[e10.get]
driver = webdriver.Chrome("/home/***/***/chromedriver")
driver.maximize_window() #driver.set_window_size(10, 10)
#1st Tab (shipco.com)
driver.get("https://tms.shipco.com/Main/Home")
driver.implicitly_wait(1)
#1st Tab Login Page
driver.find_element(By.XPATH, "//button[@id='dropdownMenuButton']").click()
driver.find_element(By.XPATH, "//input[@id='UserName']").send_keys('***')
driver.find_element(By.XPATH, "//input[@id='Password']").send_keys('***')
driver.find_element(By.XPATH, "//button[@class='btn btn-primary btn-block']").click()
#1st Tab 2nd Page
driver.find_element(By.XPATH, "//input[@id='OriginZip']").send_keys(origin_zip)
driver.find_element(By.XPATH, "//input[@id='DestinationZip']").send_keys(destination_zip)
driver.find_element(By.XPATH, "//button[@class='btn btn-secondary form-control']").click()
#Will have to add a conditional statement here to iterate through instances of multiple items/dimensions/weights
#1st Tab 3rd Page
driver.refresh();
for i in range(len(orders)):
driver.find_element(By.ID, "handlingunits").send_keys(orders[i]['handling unit'])
driver.find_element(By.ID, "quantityofgoods").send_keys(orders[i]['pieces'])
driver.find_element(By.ID, "commoditydescription").send_keys(orders[i]['description'])
driver.find_element(By.ID, "sizelength").send_keys(orders[i]['length'])
driver.find_element(By.ID, "sizewidth").send_keys(orders[i]['width'])
driver.find_element(By.ID, "sizeheight").send_keys(orders[i]['height'])
driver.find_element(By.ID, "totalweight").send_keys(orders[i]['weight'])
driver.implicitly_wait(10)
driver.find_element(By.XPATH, "//a[@class='ng-binding']").click() #this will click the object that gets class for shipment
if i < len(orders):
driver.find_element(By.XPATH, "//button[@class='btn btn-info']").click()
driver.find_element(By.XPATH, "//span[contains(text(),'Generate Smart Quote')]").click()
def insert_into_textbox(): #generates input fields for the next set of items
order_data()
#get the inputs
handling_unit = e3.get()
pieces = e4.get()
description = e5.get()
length = e6.get()
width = e7.get()
height = e8.get()
weight = e9.get()
classification = e10.get()
textbox_display = (handling_unit.ljust(11)+pieces.ljust(13)+description.ljust(11)+length.ljust(10)+width.ljust(11)+height.ljust(11)+weight.ljust(10)+classification+"\n")
textbox.insert("end",textbox_display)
e3.delete(0, "end")
e4.delete(0, "end")
e5.delete(0, "end")
e6.delete(0, "end")
e7.delete(0, "end")
e8.delete(0, "end")
e9.delete(0, "end")
e10.delete(0, "end")
master = Tk()
master.title("Quote Automator")
master.configure(background="#eef56e")
arial8 = font.Font(family="Arial", size=8, weight=font.BOLD)
algerian8 = font.Font(family="Algerian", size=8, weight=font.BOLD)
e1 = Entry(master, borderwidth=5, width=12) #origin zip
e2 = Entry(master, borderwidth=5, width=12) #destination zip
e3 = Entry(master, borderwidth=5, width=12) #handling unit(s)
e4 = Entry(master, borderwidth=5, width=12) #piece(s)
e5 = Entry(master, borderwidth=5, width=13) #description(s)
e6 = Entry(master, borderwidth=5, width=12) #length(s)
e7 = Entry(master, borderwidth=5, width=12) #width(s)
e8 = Entry(master, borderwidth=5, width=12) #height(s)
e9 = Entry(master, borderwidth=5, width=12) #weight(s)
e10 = Entry(master, borderwidth=5, width=12) #class(s)
# grid method customizes position
e1.grid(row = 0, column = 2, pady = 1, sticky="W") #origin zip
e2.grid(row = 0, column = 4, pady = 1, sticky="W") #destination zip
e3.grid(row = 3, column = 1, pady = 1, sticky="W") #handling unit(s)
e4.grid(row = 3, column = 2, pady = 1, sticky="W") #piece(s)
e5.grid(row = 3, column = 3, pady = 1, sticky="W") #description(s)
e6.grid(row = 3, column = 4, pady = 1, sticky="W") #length(s)
e7.grid(row = 3, column = 5, pady = 1, sticky="W") #width(s)
e8.grid(row = 3, column = 6, pady = 1, sticky="W") #height(s)
e9.grid(row = 3, column = 7, pady = 1, sticky="W") #weight(s)
e10.grid(row = 3, column = 8, pady = 1, sticky="W") #class(s)
textbox=Text(master, width=9, borderwidth=5, height=7)
textbox.grid(row=5, column=1, columnspan=9, sticky="nsew")
scrollbar=Scrollbar(master, orient="vertical", command=textbox.yview)
scrollbar.grid(row=5, column=10, sticky=NS)
textbox.configure(yscrollcommand=scrollbar.set)
b0 = Button(master, text = "Add Set", bg="white", fg="black", font=arial8, command=insert_into_textbox) #Add Next Set Button
b1 = Button(master, text = "Caculate Class", bg="yellow", font=arial8, command=set_class)
b2 = Button(master, text = "Get Quote", bg="#6fff00", fg="#7170ff", font=arial8, width=10, command=GetQuote)
b0.grid(row = 3, column = 0, sticky = W) # Add Next Set Button Positioning
b1.grid(row = 0, column = 8, sticky = E) # Calculate Class Button Positioning
b2.grid(row = 0, column = 7, sticky = E) # Get Quote Button Positioning
mainloop()
您可以将每一行添加到字典中,每个键都是一个数字。然后每个值可以是另一个字典或命名元组。然后您可以循环遍历方法中的数字键以获取每一行的值。
例如,您可以使用字典创建一个函数来接收数据。
orders = {}
def order_data():
# start by adding a dict to the dict with key of length dict,
# the first key will be 0, then 1, 2 and so on
orders[len(orders)] = {}
# next you can start adding data using len - 1 to access the correct key
orders[len(orders) - 1]['handling unit'] = e3.get()
# and so on for all your data
然后当您调用您的方法时,您可以使用 for 循环为每一行输入数据。
for i in range(len(orders)):
class.method(i)
然后在每个 send_keys 函数的方法中,您可以调用字典
driver.find_element(By.ID, "handlingunits").send_keys(orders[i]['handling unit'])
如果您愿意,也可以在方法内执行 for 循环。
for i in range(len(orders)):
driver.find_element(By.ID, "handlingunits").send_keys(orders[i]['handling unit'])
driver.find_element(By.ID, "quantityofgoods").send_keys(orders[i]['pieces'])
driver.find_element(By.ID, "commoditydescription").send_keys(orders[i]['description'])
driver.find_element(By.ID, "sizelength").send_keys(orders[i]['length'])
driver.find_element(By.ID, "sizewidth").send_keys(orders[i]['width'])
driver.find_element(By.ID, "sizeheight").send_keys(orders[i]['height'])
driver.find_element(By.ID, "totalweight").send_keys(orders[i]['weight'])
driver.implicitly_wait(10)
driver.find_element(By.XPATH, "//a[@class='ng-binding']").click() #this will click the object that gets class for shipment
if i < len(orders):
driver.find_element(By.XPATH, "//button[@class='btn btn-info']").click() #add another line of items if avaiable
driver.find_element(By.XPATH, "//span[contains(text(),'Generate Smart Quote')]").click()
将 'add new line' 部分放入循环中,这样的逻辑将使程序继续添加,直到数据用完,然后它会单击 'Generate Smart Quote' 按钮。