Skip to content

Instantly share code, notes, and snippets.

@codyc4321
Last active December 24, 2015 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codyc4321/787dd6f62e71cc71ae83 to your computer and use it in GitHub Desktop.
Save codyc4321/787dd6f62e71cc71ae83 to your computer and use it in GitHub Desktop.
paste me into ipython terminal
import time, subprocess
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
def gather(msg=None, default=None):
while True:
text = msg + '\n'
if default:
text += "the default is {default}...press enter to accept\n".format(default=default)
answer = raw_input(text)
return answer or default
answer = raw_input(text)
if not answer:
continue
return answer
def find_box_and_fill(html_id=None, html_name=None, value=None, driver=None):
if html_id:
box = driver.find_element_by_id(html_id)
elif html_name:
box = driver.find_element_by_name(html_name)
box.send_keys(value)
def submit_form(button_text=None, html_id=None, html_name=None, driver=None):
if button_text:
try:
form = driver.find_element_by_xpath("//input[@value='{button_text}']".format(button_text=button_text))
except:
form = driver.find_element_by_xpath("//button[normalize-space(text())='{button_text}']".format(button_text=button_text))
form.submit()
elif html_name:
form = driver.find_element_by_name(html_name)
form.submit()
elif html_id:
form = driver.find_element_by_id(html_id)
form.submit()
driver = webdriver.Firefox()
driver.get("https://jsfiddle.net/user/login/")
time.sleep(2)
username = gather(msg="what is your jsfiddle username?")
password = gather(msg="what is your jsfiddle password?")
find_box_and_fill(html_id="id_username", value=username, driver=driver)
find_box_and_fill(html_name="password", value=password, driver=driver)
submit_form(button_text="Log in", driver=driver)
driver.get("http://jsfiddle.net/")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment