Reputation: 401
I have written a python script that communicates with the web page and makes an online order. In addition to that I have added some GUI, which allows the user to select the day of the order. Here is the python script
import easygui
import sys
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import tkinter as tk
import tkinter.font as tkFont
badge_number = 111111
# Widget pop up for selection of the order day
root = tk.Tk()
def center_window(w=300, h=200):
# get screen width and height
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
# calculate position x, y
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root.geometry("400x450")
Label = tk.Label(root, text = "Select lunch Order Day", font = ("Helvetica", 15))
Label.pack(pady=50, padx=40)
helv15 = tkFont.Font(family = "Helvetica", size = 15)
root.lift()
v = tk.StringVar()
v.set("Today")
tk.Label(root, textvariable = v).pack()
def close_window():
root.destroy()
today = tk.Radiobutton(root, text = "Today", variable = v, value = "Today", font = helv15).pack()
tomorrow = tk.Radiobutton(root, text = "Tomorrow", variable = v, value = "Tomorrow", font = helv15).pack()
cancel = tk.Radiobutton(root, text = "Cancel", variable = v, value = "Cancel", font = helv15).pack()
submit = tk.Button(root, text = "Submit", command = close_window, font = helv15).pack()
center_window(400, 400)
root.mainloop()
if v.get() == "Cancel":
sys.exit("Ordering was terminated")
driver = webdriver.Chrome(executable_path = "C:\webdrivers\chromedriver.exe")
driver.get("http://web_page/")
element = driver.find_element_by_name('employee_id')
element.send_keys(badge_number)
driver.find_element_by_xpath("/html/body/section/header/form/div/button").click()
# /html/body/section/header/form/div/button
delay = 60
todayBut = "/html/body/section/div[1]/div[2]/div/div/div/span[2]/span[1]/form/button"
tomorBut = "/html/body/section/div[1]/div[2]/div/div/div/span[1]/span[1]/form/button"
favorButToday = "/html/body/section/div/div[1]/button"
favorButTomor = "/html/body/section/div/div[1]/button/a"
myFavOrderID = "fav_order_view"
try:
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, todayBut)))
except TimeoutException:
print ("Loading Day Selection page took too much time!")
if v.get() == "Today":
driver.find_element_by_xpath(todayBut).click()
#favorite choice
try:
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, favorButToday)))
driver.find_element_by_xpath(favorButToday).click()
try:
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, myFavOrderID)))
driver.find_element_by_id(myFavOrderID).click()
### select floor, order date and order now
select_floor = Select(driver.find_element_by_name("floor_number"))
select_floor.select_by_index("9")
select_day = Select(driver.find_element_by_name("order_date"))
select_day.select_by_value("today")
orderNowBtn = "/html/body/div[1]/div[2]/div/div/div[3]/form/button"
driver.find_element_by_xpath(orderNowBtn).click()
print("Today's order was successfully made!")
except TimeoutException:
print ("Loading Favorites page took too much time!")
except TimeoutException:
print ("Loading took too much time!")
else:
driver.find_element_by_xpath(tomorBut).click()
#favorite choice
try:
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, favorButTomor)))
driver.find_element_by_xpath(favorButTomor).click()
try:
myFavOrderIDTomor = "fav_order_view"
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, myFavOrderIDTomor)))
driver.find_element_by_id(myFavOrderIDTomor).click()
### select floor, order date and order now
select_floor = Select(driver.find_element_by_name("floor_number"))
select_floor.select_by_index("9")
select_day = Select(driver.find_element_by_name("order_date"))
select_day.select_by_value("tomorrow")
orderNowBtnTomor = "/html/body/div[1]/div[2]/div/div/div[3]/form/button"
driver.find_element_by_xpath(orderNowBtnTomor).click()
print("Tomorrow's order was successfully made!")
except TimeoutException:
print ("Loading Favorites page took too much time!")
except TimeoutException:
print ("Loading took too much time!")
However, I wanted to have it as an .exe file to run it with just double-click from my desktop. So, I have installed pyinstaller
from git and run the simple line in the cmd (pyinstaller.exe --onefile --windowed --icon=app.ico LunchOrder.py
). Unfortunately, the .exe isn't doign anything, it just pops up the "Fatal error detected"
message, without any GUI (that I have written into the script itself). Any help would be appreciated!
Upvotes: 0
Views: 504
Reputation: 15098
Here i will tell you a better way to make .py to .exe . First make sure your python is added to system PATH, which i assume is. If not check some tutorials on how to add python to PATH then you can open your cmd and type in:
pip install pyinstaller
After this, you go to your path where the LunchOrder.py exists and open a cmd there, in windows you can type simply type in cmd in the address bar (after clearing adressbar), or else open a cmd and navigate to your path where LunchOrder.py file exists and simply type in:
pyinstaller -w -F -i "app.ico" LunchOrder.py
This should make a folder dist inside which your .exe will be there. MAKE SURE TO COPY THE .exe to outside the dist folder and run it there. Or else you might still get fatal error message. You can delete all the other extra files that was generated like build and LunchOrder.spec . If it still gives you error try following this method https://www.youtube.com/watch?v=OZSZHmWSOeM
Upvotes: 1