Last active
November 1, 2016 03:17
-
-
Save dattasaurabh82/828af2c1d6ae2cb2dfd7d1ae4b464e08 to your computer and use it in GitHub Desktop.
Browser mechanization with python and terminal calls to applescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import time | |
tabs = 0 | |
#----------------- open a new tab | |
def open_new_tab(serach_website): | |
new_tab_cmd = "osascript -e 'tell application \"Google Chrome\" to open location \"https://" + serach_website + "\" & activate'" | |
# this will be executed from a shell moment and will open 3 tabs | |
new_tab_code = subprocess.call(new_tab_cmd, shell=True) | |
#----------------- activation function for a tab of interest | |
# Healty practice; Can use later to discretely activate a specific tab | |
def activate_tab(tab_number): | |
activate_tab_cmd = "osascript -e 'tell application \"Google Chrome\" to set (active tab index of (window 1)) to " + str(tab_number) + "'" | |
active_tab_code = subprocess.call(activate_tab_cmd, shell=True) | |
#----------------- counting tabs - healty practice can use later for discretely activate a specific tab | |
def count_current_tabs(): | |
count_tabs_cmd = "osascript -e 'tell application \"Google Chrome\" to count (every tab of window 1)'" | |
count_tabs_result = subprocess.check_output(count_tabs_cmd, stderr=subprocess.STDOUT, shell=True) | |
count_tabs_result = count_tabs_result[:-1] | |
return int(count_tabs_result) | |
#----------------- activation function for a tab of interest | |
def activate_tab(tab_number): | |
activate_tab_cmd = "osascript -e 'tell application \"Google Chrome\" to set (active tab index of (window 1)) to " + str(tab_number) + "'" | |
active_tab_code = subprocess.call(activate_tab_cmd, shell=True) | |
#----------------- typing function | |
def emulate_words_onsearch_tab(search_term): | |
typein_stuff_cmd = "osascript -e 'tell application \"System Events\" to keystroke \"" + search_term + "\" & return'" | |
type_in_code = subprocess.call(typein_stuff_cmd, shell=True) | |
#----------------- deleting function | |
def emulate_delete(times): | |
for i in range(0, times): | |
typein_stuff_cmd = "osascript -e 'tell application \"System Events\" to key code 51'" | |
type_in_code = subprocess.call(typein_stuff_cmd, shell=True) | |
#----------------- DEMO usage | |
open_new_tab("www.google.com") | |
last_tab = count_current_tabs() | |
activate_tab(last_tab) # takes an argument i.e tab number. I calutaed it earlier | |
time.sleep(7) # waiting for the url to load properly at slow internet | |
emulate_words_onsearch_tab("afterlife") # takes an argument of the search terms | |
time.sleep(6) | |
open_new_tab("www.duckduckgo.com") | |
last_tab = count_current_tabs() | |
activate_tab(last_tab) | |
time.sleep(7) | |
emulate_words_onsearch_tab("ghosts are immutables") | |
time.sleep(6) | |
activate_tab(last_tab - 1) | |
time.sleep(3) | |
emulate_delete(len("afterlife")) | |
time.sleep(3) | |
emulate_words_onsearch_tab("knobs and brains") | |
# And then you can use web browser to click links and all that stuff | |
#----------------- checking if they have loaded or not | |
# third_last_tab_number = tabs - 2 | |
# last_tab_number = tabs + 1 | |
# loading_states = [True, True, True] | |
# # if either of the laoding states is true | |
# if loading_states[0] == True or loading_states[1] == True or loading_states[2] == True: | |
# # iterate through all of the tabs and check their loading states | |
# for i in range(third_last_tab_number, last_tab_number): | |
# get_prop_code = "osascript -e 'tell application \"Google Chrome\" to loading of tab " + str(i) + " of window 1'" | |
# get_loading = subprocess.check_output(get_prop_code, shell=True) | |
# # it's returned as a string | |
# state_of_loading = get_loading[:-1] # we are getting rid of last char in the '<state>\n' to make it '<state>'' | |
# print state_of_loading | |
# if state_of_loading == "False": | |
# loading_states[i+1-i] = False | |
# else: | |
# loading_states[i+1-i] = True | |
# time.sleep(2) | |
# if state_of_loading == "false": | |
# print state_of_loading | |
# elif (keep_checking[tabs] == False && keep_checking[tabs - 1] == False && keep_checking[tabs - 2] == False): | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment