so I've made a script with Selenium that inputs into a form details from a CSV. The goal is to make a GUI now and I've done that too , the tricky part now that I can't wrap my head around is the combining of the two.
My GUI:
import PySimpleGUI as sg
def make_window(theme):
sg.theme(theme)
menu_def = [['&Application', ['E&xit']],
['&Help', ['&About']] ]
right_click_menu_def = [[], ['Exit']]
# Table Data
input_layout = [[sg.Menu(menu_def, key='-MENU-')],
[sg.Button("Open File")],
[sg.Text('Chrome Version')],
[sg.OptionMenu(values=('96', '97', '98'), k='-OPTION MENU-'),],
[sg.Button('Submit')]]
layout = [[sg.Text('Email', size=(38, 1), justification='center', font=("Helvetica", 16), relief=sg.RELIEF_RIDGE, k='-TEXT HEADING-', enable_events=True)]]
layout =[[sg.TabGroup([[ sg.Tab('Setup CSV and Chrome Version', input_layout),
]], key='-TAB GROUP-')]]
return sg.Window('Email', layout, right_click_menu=right_click_menu_def)
def main():
window = make_window(sg.theme())
# This is an Event Loop
while True:
event, values = window.read(timeout=100)
# keep an animation running so show things are happening
if event not in (sg.TIMEOUT_EVENT, sg.WIN_CLOSED):
print('============ Event = ', event, ' ==============')
print('-------- Values Dictionary (key=value) --------')
for key in values:
print(key, ' = ',values[key])
if event in (None, 'Exit'):
print("[LOG] Clicked Exit!")
break
elif event == 'About':
print("[LOG] Clicked About!")
sg.popup('email',
'Select CSV file',
'Select Chrome Version',
'Submit',
'')
elif event == 'Popup':
print("[LOG] Clicked Popup Button!")
sg.popup("You pressed a button!")
print("[LOG] Dismissing Popup!")
elif event == "Open File":
print("[LOG] Clicked Open File!")
folder_or_file = sg.popup_get_file('Choose your file')
sg.popup("You chose: " str(folder_or_file))
print("[LOG] User chose file: " str(folder_or_file))
window.close()
exit(0)
if __name__ == '__main__':
main()
My script:
#-------------------------------------------------------------------------------
# Imports
import csv
import requests
from selenium import webdriver
import time
#-------------------------------------------------------------------------------
# Setup
with open('data.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
#-------------------------------------------------------------------------------
# Web Automation
driver = webdriver.Chrome(executable_path='./chromedriver.exe')
driver.get('site/')
title_field = driver.find_element_by_xpath('//*[@id="TITLE"]')
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"]')
depost_field = driver.find_element_by_xpath('//*[@id="DEPOSIT"]')
submit = driver.find_element_by_xpath('//*[@id="sib-form"]/div[9]/div/button')
for line in csv_reader:
time.sleep(3)
title_field.send_keys(line[0])
fname_field.send_keys(line[1])
lname_field.send_keys(line[2])
phone_field.send_keys(line[3])
mail_field.send_keys(line[4])
depost_field.send_keys(line[5])
submit.click()
#-------------------------------------------------------------------------------
From the GUI I want to select the CSV file, and use an if and else on the options to select the appropriate chromedriver.exe file, submit to run the app. I know I'm going to have to make the automation part into a function however where do I place the automation code and the arguments inplace for the file, chromeversion and conditional ?
Any and all help appreciated.
CodePudding user response:
When Submit
button clicked, then go your my script
. It maybe like this
import threading
import selenium
import PySimpleGUI as sg
def run_selenium(window, file, driver):
# My script
with open(file, 'rt') as csv_file:
csv_reader = csv.reader(csv_file)
driver = webdriver.Chrome(executable_path=driver)
...
# Not to update GUI in thread, but generate an event which will be processed in event loop.
window.write_event_value('Done', None)
def main():
# My GUI
window = make_window(sg.theme())
folder_or_file = None
paths = {
'96': './chromedriver96.exe',
'97': './chromedriver97.exe',
'98': './chromedriver98.exe',
}
while True:
event, values = window.read(timeout=100)
...
elif event == 'Submit':
version = values['-OPTION MENU-']
if folder_or_file is None or version not in paths:
print("No CSV file selected or wrong Chrome version selected")
continue
# Using thread to avoid long time job will block and cause GUI no response
threading.Thread(target=run_selenium, args=(window, folder_or_file, paths[version])).start()
window['Submit'].update(disabled=True)
print('[LOG] Run Selenium ...')
elif event == 'Done':
window['Submit'].update(disabled=False)
print('[LOG] End Selenium')
window.close()
CodePudding user response:
Using tkinter and configparser
You can get tkinter with pip install tk
gui.py
import tkinter as tk
import tkinter.filedialog as file_dialog
import threading
import your_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=your_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("My Window")
window.geometry('350x200')
panel = tk.Frame(window)
panel.pack(expand=True)
lbl = tk.Label(window, text="CSV FILE")
lbl.pack(expand=True)
btnCSV = tk.Button(window, text="Select",command=lambda: open_file_dialog(csv_path=True))
btnCSV.pack(expand=True)
lbl = tk.Label(window, text="Chromedriver")
lbl.pack(expand=True)
btnCD = tk.Button(window, text="Select",command=lambda: open_file_dialog(chromedriver_path=True))
btnCD.pack(expand=True)
btnCD = tk.Button(window, text="Start",command=start_your_script)
btnCD.pack(expand=True)
tk.mainloop()
and wrap your script in a function called start like
# -------------------------------------------------------------------------------
# Imports
import csv
import requests
from selenium import webdriver
import time
# -------------------------------------------------------------------------------
# Setup
def start(data,exe_path):
with open(data, 'r') as csv_file:
csv_reader = csv.reader(csv_file)
# -------------------------------------------------------------------------------
# Web Automation
driver = webdriver.Chrome(executable_path=exe_path)
driver.get('site/')
ids = "TITLE FIRSTNAME LASTNAME PHONE EMAIL DEPOSIT".split()
for line in csv_reader:
time.sleep(0.2)
for i,id in enumerate(ids):
driver.find_element_by_xpath(f'//*[@id="{id}"]').send_keys(line[i])
#submit
driver.find_element_by_xpath('//*[@id="sib-form"]/div[9]/div/button').click()