如何遍历多组数据并将数据重复输入到方法中? (tkinter,硒,python)

问题描述 投票:0回答:1

我正在尝试编写一个自动化程序,该程序将从单个UI接收用户输入的数据,并将该数据自动输入到单个网站或多个网站中。

UI

当按下“获取报价”按钮时,将打开chrome浏览器会话,并将来自UI的所有数据放入网站中其相应的输入字段中。

browsersessionexample

我现在要弄清楚的是,如何处理多于一组数据的情况。例如:

UIexample2

这里是我希望程序能够执行的操作的图片。基本上输入第二组数据,然后单击“添加行项目”,如果没有任何数据可输入,则脚本将自动单击“生成智能报价”。

websessionexamplescenario

能够输入第二或第三组数据的最佳方法是什么?数据必须以相同的方法重新输入。这是上下文的一些代码。

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

    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()    

def insert_into_textbox(): #generates input fields for the next set of items

    #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 Next 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() 
python-3.x selenium tkinter automation webautomation
1个回答
0
投票

您可以执行类似将每一行添加到字典中的操作,每个键都是一个数字。然后,每个值可以是另一个字典或命名的元组。然后,您可以遍历方法中的数字键以获取每一行的值。

例如,使用字典可以使函数接收数据。

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循环。

© www.soinside.com 2019 - 2024. All rights reserved.