0

我的应用程序中有三个文件:一个用于 Tkinter,一个用于脚本,最后一个用于配置。我已经尝试过,但根本无法将文件合二为一,我只是找不到。我的 Tkinter 代码:

import tkinter as tk
import tkinter.filedialog as file_dialog
import threading
import SCRIPT
import configparser


#class Config helps writing and reading from config.ini file.
class Config:
    def __init__(self):
        self.config = configparser.ConfigParser()
        self.read()

    def set(self, key, value):
        self.config.set('setup', key, value)
        self.write()

    def write(self):
        with open('config.ini', 'w') as f:
            self.config.write(f)

    def read(self):
        self.config.read('config.ini')

    def get(self, key):
        self.read()
        return self.config.get('setup', key)


def start_your_script():
    threading.Thread(target=SCRIPT.start,args=(Config().get("csv_path"), Config().get("exe_path"))).start()


def open_file_dialog(chromedriver_path=False,csv_path=False):
    if csv_path:
        path = file_dialog.askopenfilename(filetypes=[("CSV Files", "*.csv")])
        Config().set("csv_path",path)

    elif chromedriver_path:
        path = file_dialog.askopenfilename(filetypes=[("Applications", "*.exe")])
        Config().set("exe_path",path)

Config().write()
window = tk.Tk()
window.title("")
window.geometry('350x200')
window.configure(background='#ffffff')

panel = tk.Frame(window)
panel.pack(expand=True)

lbl = tk.Label(window, text="CSV File", font=("Arial Bold", 12),fg='white',bg='lightblue',width=12)
lbl.pack(expand=True)


btnCSV = tk.Button(window, text="Select",command=lambda: open_file_dialog(csv_path=True),font=("Arial Bold", 12))
btnCSV.pack(expand=True)

lbl = tk.Label(window, text="Chrome Driver",font=("Arial Bold", 12),fg='white',bg='lightblue',width=12)
lbl.pack(expand=True)

btnCD = tk.Button(window, text="Select",command=lambda: open_file_dialog(chromedriver_path=True),font=("Arial Bold", 12))
btnCD.pack(expand=True)

btnCD = tk.Button(window, text="Start",command=start_your_script,font=("Arial Bold", 12),fg="white",bg="green",width=15)
btnCD.pack(expand=True)

tk.mainloop()

还有我的脚本代码:

# -------------------------------------------------------------------------------
# Imports
import csv
import requests
from selenium import webdriver
import time
# -------------------------------------------------------------------------------
# Setup

def start(data,exe_path):

    driver = webdriver.Chrome(executable_path=exe_path)
        #add this line
    driver.implicitly_wait(10)
 
    driver.get('http:///')

    fname_field = driver.find_element_by_xpath('//*[@id="FIRSTNAME"]')
    lname_field = driver.find_element_by_xpath('//*[@id="LASTNAME"]')
    phone_field = driver.find_element_by_xpath('//*[@id="PHONE"]')
    mail_field = driver.find_element_by_xpath('//*[@id="EMAIL"]')
    deposit_field = driver.find_element_by_xpath('//*[@id="DEPOSIT"]')
    submit = driver.find_element_by_xpath('//*[@id="sib-form"]/div[8]/div/button')
        
    with open(data, 'r') as csv_file:
        csv_reader = csv.reader(csv_file,delimiter=';')
        # -------------------------------------------------------------------------------
        # Web Automation

        next(csv_reader)
        for line in csv_reader:
            
            time.sleep(1.8)
            
            fname_field.send_keys(line[10])
            lname_field.send_keys(line[11])
            mail_field.send_keys(line[13])
            phone_field.send_keys(line[16])
            deposit_field.send_keys(line[37])
            submit.click()
            # wait for notifcation --> driver.implicitly_wait(10) sets up the driver so it will wait for 10 seconds
            try:
                if "Bidder added successfully" == driver.find_element_by_class_name('sib-form-message-panel__inner-text').text:
                    # close the notifcation or just continue
                    pass
                else:
                    #bidder wasn't added succesfully
                    pass
            except:
                print("driver didn't find the element with 10 seconds wait period")
        driver.quit()

最后是我的配置文件

[setup]

我已经尝试了几个小时才能使它正常工作,请感谢所有帮助。

4

0 回答 0