Advertisement
Guest User

Untitled

a guest
Jul 31st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. import json
  2.  
  3. from selenium import webdriver
  4.  
  5.  
  6. def get_credentials():
  7. """Get user credentials from a JSON file"""
  8. with open('auth.json', 'r') as f:
  9. auth = json.load(f)
  10. username = auth['username']
  11. password = auth['password']
  12. return username, password
  13.  
  14.  
  15. class LeapHandler:
  16. """Class to handle any interactions with Leap Card website"""
  17.  
  18. def __init__(self):
  19. self.driver = webdriver.Chrome()
  20. self.driver.get('https://www.leapcard.ie/en/login.aspx')
  21. self.login()
  22.  
  23. def get_login_elements(self):
  24. """Find and return elements necessary to log in"""
  25. username_id = 'ContentPlaceHolder1_UserName'
  26. password_id = 'ContentPlaceHolder1_Password'
  27. login_id = 'ContentPlaceHolder1_btnlogin'
  28. username_field = self.driver.find_element_by_id(username_id)
  29. password_field = self.driver.find_element_by_id(password_id)
  30. login_button = self.driver.find_element_by_id(login_id)
  31. return username_field, password_field, login_button
  32.  
  33. def login(self):
  34. """Login into user account"""
  35. username_field, password_field, login = self.get_login_elements()
  36. username, password = get_credentials()
  37. username_field.send_keys(username)
  38. password_field.send_keys(password)
  39. login.click()
  40.  
  41. def get_balance(self):
  42. """Return current Leap Card Balance"""
  43. balance = self.driver.find_element_by_css_selector('div.pull-left').text
  44. return balance
  45.  
  46. if __name__ == '__main__':
  47. lh = LeapHandler()
  48. balance = lh.get_balance()
  49. print(f'LepaCard balance is €{balance}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement