Instantly share code, notes, and snippets.

@kocsenc /pisland.py
Last active Dec 12, 2015

Embed
What would you like to do?
Island Script
#!/usr/bin/env python
from time import sleep
from selenium.common.exceptions import TimeoutException, StaleElementReferenceException
from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import sys
__author__ = 'kocsen'
def main():
"""
Makes a new firefox instance, logs in, prompts before starting breed for
user to take the browser to the appropriate place.
:return:
"""
d = webdriver.Firefox()
w = WebDriverWait(d, 5) # Waiting mechanism
url = "http://ponyisland.net"
d.get(url)
in_user = raw_input("Username: ")
in_pass = raw_input("Password: ")
user_field = _get(w, '//*[@id="login"]/form/fieldset[1]/div/input')
user_pass = _get(w, '//*[@id="login"]/form/fieldset[2]/div/input')
user_field.clear()
user_pass.clear()
user_field.send_keys(in_user)
user_pass.send_keys(in_pass)
_get(w, '//*[@id="login"]/form/div/button').click()
breed(w, d)
def breed(w, d):
"""
Actually breeds the ponies, by clicking the breed, confirming the modal and
then clicking next.
:param w:
:param d:
:return:
"""
raw_input("Press enter when you have arrived at breeding page with the correct pony breed.")
in_do_left = raw_input("\nWhat pony side?\n Press [l] for left or [r] for right (default): ")
do_left = in_do_left.lower().startswith('l')
if do_left:
breeder = '//*[@id="breeding"]/div/form/fieldset[2]/ul/li[1]/div/a'
else:
breeder = '//*[@id="breeding"]/div/form/fieldset[3]/ul/li[1]/div/a'
confirm = '/html/body/div[2]/div[3]/div/button[1]'
next_pony = '//*[@id="profile"]/h3[1]/a[1]'
pony_name = '//*[@id="profile"]/h3[1]/span/p/a'
try:
while True:
try:
print("Breeding pony: " + str(_get(w, pony_name).text))
except StaleElementReferenceException:
sleep(3)
print("Breeding pony whose name I couldn't find!")
_get(w, breeder).click()
_get_confirm(d).click()
_get(w, next_pony).click()
sleep(1)
except TimeoutException:
print("It seems there was an error or we finished with the pony life!")
def _get(w, xpath):
return w.until(EC.presence_of_element_located((By.XPATH, xpath)))
def _get_confirm(d):
for button in d.find_elements_by_tag_name('button'):
try:
if button.find_element_by_tag_name('span').text == "Ok":
return button
except:
pass
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment