def apply_style(driver, element):
driver.execute_script("arguments[0].setAttribute('style', arguments[1]);",
element, "background: yellow; border: 2px solid red;")
original_style = element.get_attribute('style')
def highlight(element):
"""Highlights (blinks) a Selenium Webdriver element"""
driver = element._parent
def apply_style(s):
driver.execute_script("arguments[0].setAttribute('style', arguments[1]);",
element, s)
original_style = element.get_attribute('style')
apply_style("background: yellow; border: 2px solid red;")
import string
def generate_tags(number):
letters = list(string.ascii_lowercase)
letters.extend([i+b for i in letters for b in letters])
return letters[:number]
def create_markers(controller):
script = """
var btn = document.createElement("DIV");
var t = document.createTextNode("");
btn.appendChild(t);
btn.id = "emacsControllerClickables"
"""
elem = """
var span = document.createElement("SPAN")
var t = document.createTextNode("{}")
span.appendChild(t)
span.style.cssText = "position: absolute; display: block; top: {}px; left: {}px; white-space: nowrap; overflow: hidden; font-size: 12px; padding: 1px 3px 0px 3px; background: linear-gradient(to bottom, #FF1493 0%,#FF69B4 100%); border: solid 1px #C38A22; border-radius: 3px; box-shadow: 0px 3px 7px 0px rgba(0, 0, 0, 0.3); color: white; z-index: 10000"
btn.appendChild(span)
"""
mapping = {}
clickables = controller.find_elements_by_tag_name("a")
clickables = clickables + controller.find_elements_by_tag_name("button")
clickables = clickables + controller.find_elements_by_tag_name("input")
tags = generate_tags(len(clickables))
for element in clickables:
tag_name = tags.pop()
mapping[tag_name] = element
script = script + elem.format(
tag_name,
element.location["y"],
element.location["x"])
script = script + "document.body.appendChild(btn);"
controller.execute_script(script)
return mapping
def select_marker(tag, markers):
return markers[tag]
def close_markers(controller):
controller.execute_script("""var element = document.getElementById("emacsControllerClickables");
element.parentNode.removeChild(element);""")
return True
def switch_window(controller):
try:
current_window = controller.current_window_handle
except:
current_window = controller.window_handles[0]
titles = {}
for window in controller.window_handles:
controller.switch_to.window(window)
titles[window] = controller.title
controller.switch_to.window(current_window)
for title in titles.values():
print(title)
return titles
def switch_window_switch(value, windows):
for window in windows:
if windows[window] == value:
selected = window
controller.switch_to_window(selected)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import json
if browser == "firefox":
controller = webdriver.Firefox()
elif browser == "safari":
controller = webdriver.Safari()
elif browser == "chrome":
controller = webdriver.Chrome()
else:
raise BaseException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
element = select_marker("%s", markers)
switch_window_switch("%s", windows)
element.get_attribute("id")
element.get_attribute("class")
element.get_attribute("name")
element.get_attribute("href")
ActionChains(controller).key_down(Keys.PAGE_DOWN).perform()
ActionChains(controller).key_down(Keys.PAGE_UP).perform()
ActionChains(controller).key_down(Keys.LEFT).perform()
ActionChains(controller).key_down(Keys.RIGHT).perform()
ActionChains(controller).key_down(Keys.UP).perform()
ActionChains(controller).key_down(Keys.DOWN).perform()
ActionChains(controller).send_keys("%s").perform()
controller.execute_script("window.open('https://google.com');")
ActionChains(controller).send_keys(Keys.ENTER).perform()
ActionChains(controller).send_keys(Keys.ESCAPE).perform()
ActionChains(controller).send_keys(Keys.TAB).perform()
element = controller.find_element_by_xpath("//*[contains(text(), '%s')]")
temp = controller.find_elements(By.TAG_NAME, "body")
temp = temp[0]
print temp.text
controller.maximize_window()
resolution = controller.get_window_size()
controller.set_window_size(resolution['width'], resolution['height'] * .8)
markers = create_markers(controller)
close_markers(controller)
windows = switch_window(controller)
print(" ".join(markers.keys()))
print(controller.page_source)
for cookie in controller.get_cookies():
print(json.dumps(cookie))
controller.delete_cookie("%s")
controller.add_cookie((json.loads('%s')))
element = WebDriverWait(controller, 20).until(EC.presence_of_element_located((By.ID, "%s")))
element = WebDriverWait(controller, 20).until(EC.presence_of_element_located((By.CLASS_NAME, "%s")))
element = WebDriverWait(controller, 20).until(EC.presence_of_element_located((By.NAME, "%s")))
element = WebDriverWait(controller, 20).until(EC.presence_of_element_located((By.TAG_NAME, "%s")))
element = WebDriverWait(controller, 20).until(EC.presence_of_element_located((By.LINK_TEXT, "%s")))
element = controller.find_element_by_id("%s")
element = controller.find_element_by_class_name("%s")
element = controller.find_element_by_name("%s")
element = controller.find_element_by_tag_name("%s")
element = controller.find_element_by_link_text("%s")
element = controller.get("%s")
input('Press enter to to continue.')