-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathAuto_Chrome_2.py
181 lines (151 loc) · 6.02 KB
/
Auto_Chrome_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import subprocess
from tkinter import *
from tkinter.messagebox import WARNING, showinfo
from tkinter.filedialog import askopenfilename, asksaveasfilename
import os
import sys
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as ExpectedConditions
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.relative_locator import locate_with
import platform
opsys = platform.system()
main_directory = os.getcwd()
file = None
root = Tk()
root.title("Chrome Automation")
root.geometry("644x788")
def open_chrome_profile():
if opsys == "Windows":
subprocess.Popen(['start', 'chrome', '--remote-debugging-port=8989', '--user-data-dir=' + main_directory + '/chrome_profile'], shell=True)
elif opsys == "Darwin":
cmd = "open -a /Applications/Google\ Chrome.app --args --remote-debugging-port=8989 --user-data-dir={cwd}/chrome_profile".format(cwd = main_directory)
subprocess.Popen(cmd, shell=True)
else:
subprocess.Popen(['google-chrome', '--remote-debugging-port=8989', '--user-data-dir=' + main_directory + '/chrome_profile'], shell=True)
#####MENU BAR#####
def newFile():
global file
root.title("Untitled - Auto_Chrome_2")
file = None
TextArea.delete(1.0, END)
def openFile():
global file
file = askopenfilename(defaultextension=".txt",
filetypes=[("All Files", "*.*"),
("Text Documents", "*.txt")])
if file == "":
file = None
else:
root.title(os.path.basename(file) + " - Auto_Chrome_2")
TextArea.delete(1.0, END)
f = open(file, "r")
TextArea.insert(1.0, f.read())
f.close()
def saveFile():
global file
if file == None:
file = asksaveasfilename(initialfile = 'Untitled.txt', defaultextension=".txt",
filetypes=[("All Files", "*.*"),
("Text Documents", "*.txt")])
if file =="":
file = None
else:
#Save as a new file
f = open(file, "w")
f.write(TextArea.get(1.0, END))
f.close()
root.title(os.path.basename(file) + " - Auto_Chrome_2")
print("File Saved")
else:
# Save the file
f = open(file, "w")
f.write(TextArea.get(1.0, END))
f.close()
def quitApp():
root.destroy()
def donate():
showinfo("Donate", "https://linktr.ee/Cloudmaking")
def readme():
showinfo("ReadMe", "Comand List:\ngo_to(adress)\ncss_and_key(code, key)\nxpath_and_key(code, key)\ncss_and_click(code)\nxpath_and_click(code)\nlinktext_key(code, key)\nlinktext_click(code)")
def about():
showinfo("Chrome Automator", "Auto_Chrome by @Cloudmaking")
MenuBar = Menu(root)
FileMenu = Menu(MenuBar, tearoff=0)
FileMenu.add_command(label="New", command=newFile)
FileMenu.add_command(label="Open", command = openFile)
FileMenu.add_command(label = "Save", command = saveFile)
FileMenu.add_separator()
FileMenu.add_command(label = "Exit", command = quitApp)
MenuBar.add_cascade(label = "File", menu=FileMenu)
HelpMenu = Menu(MenuBar, tearoff=0)
HelpMenu.add_command(label = "Donate", command=donate)
HelpMenu.add_command(label = "ReadME", command=readme)
HelpMenu.add_command(label = "About Notepad", command=about)
MenuBar.add_cascade(label="Help", menu=HelpMenu)
root.config(menu=MenuBar)
#####MENU BAR END#####
# _____MAIN_CODE_____
def main_program_loop():
###new chrome options
if opsys == "Windows":
ser = Service(main_directory + "/chromedriver.exe")
elif opsys == "Darwin":
ser = Service(main_directory + "/chromedriver")
else:
ser = Service(main_directory + "/linuxchromedriver")
op = webdriver.ChromeOptions()
op.add_experimental_option("debuggerAddress", "localhost:8989")
driver = webdriver.Chrome(service=ser, options=op)
wait = WebDriverWait(driver, 60)
#establish main page
main_page = driver.current_window_handle
def xpath_and_click(code):
x = wait.until(ExpectedConditions.presence_of_element_located((By.XPATH, code)))
x.click()
def css_and_click(code):
x = wait.until(ExpectedConditions.presence_of_element_located((By.CSS_SELECTOR, code)))
x.click()
def xpath_and_key(code, key):
x = wait.until(ExpectedConditions.presence_of_element_located((By.XPATH, code)))
x.send_keys(key)
time.sleep(1)
def css_and_key(code, key):
x = wait.until(ExpectedConditions.presence_of_element_located((By.CSS_SELECTOR, code)))
x.send_keys(key)
time.sleep(1)
def linktext_click(code):
x = wait.until(ExpectedConditions.presence_of_element_located((By.LINK_TEXT, code)))
x.click()
time.sleep(1)
def linktext_key(code, key):
x = wait.until(ExpectedConditions.presence_of_element_located((By.LINK_TEXT, code)))
x.send_keys(key)
time.sleep(1)
def go_to(adress):
driver.get(adress)
exec(TextArea.get(1.0, END))
####UI ELEMENTS######
my_font = "Arial Black"
info = Label(text = "Make sure the browser is open before starting or the program might freeze/crash", bg = "white")
info.pack()
info.config(font=("Arial", 9))
open_browser = Button(text = "Open Browser", command = open_chrome_profile, bg = "yellow", font=(my_font, 10))
open_browser.pack(anchor=N, pady=5, padx=5)
start_button = Button(text = "Start", command = main_program_loop, bg = "green", font=(my_font, 10))
start_button.pack(anchor=N, pady=5, padx=5)
#Add TextArea
TextArea = Text(root, font=("Arial", 10), bg = "light gray")
TextArea.pack(expand=True, fill=BOTH, anchor=S)
#scrooll bar
Scroll = Scrollbar(TextArea)
Scroll.pack(side=RIGHT, fill=Y)
Scroll.config(command=TextArea.yview)
TextArea.config(yscrollcommand=Scroll.set)
####UI ELEMENTS END####
root.mainloop()