Skip to content

Instantly share code, notes, and snippets.

@philpownall
Created December 20, 2016 17:35
Show Gist options
  • Save philpownall/a84956460b68f08dee66c2496d7ef7fa to your computer and use it in GitHub Desktop.
Save philpownall/a84956460b68f08dee66c2496d7ef7fa to your computer and use it in GitHub Desktop.
Hydro.py Selenium Webdriver script to extract usage data and graphs from Hydro One
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Hydro(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.myaccount.hydroone.com/"
self.verificationErrors = []
self.accept_next_alert = True
def test_hydro(self):
f = open('HydroData.txt', 'a')
driver = self.driver
driver.get(self.base_url + "/seleniumhq/selenium-google-code-issue-archive/issues/5686")
driver.get(self.base_url + "/unsecure/EC/ecustomer/en/user/ECCustomerSignInPage.html")
driver.find_element_by_name("userid").clear()
driver.find_element_by_name("userid").send_keys("<your userid here>")
driver.find_element_by_name("password").clear()
driver.find_element_by_name("password").send_keys("<your password here>")
driver.find_element_by_css_selector("input[type=\"image\"]").click()
time.sleep(10)
driver.find_element_by_link_text("My Usage >").click()
time.sleep(25)
f.write("\"" + time.strftime("%c") + "\",")
driver.save_screenshot('HydroUsage.png')
driver.find_element_by_xpath("//div[@id='ctl00_ctl00_TopNav1_tabMainNav']/div/ul/li[2]/a/span/span/span").click()
time.sleep(10)
HourlyUsage = driver.find_element_by_id("ctl00_ctl00_ContentPlaceHolder1_summaryContent_lblTotalUsage").text
HourlyCost = driver.find_element_by_id("ctl00_ctl00_ContentPlaceHolder1_summaryContent_lblTotalCost").text
f.write("\"" + HourlyUsage + "\",\"" + HourlyCost + "\",")
driver.save_screenshot('HydroHourlyUsage.png')
driver.find_element_by_xpath("//div[@id='ctl00_ctl00_TopNav1_tabMainNav']/div/ul/li[3]/a/span/span/span").click()
time.sleep(10)
DailyUsage = driver.find_element_by_id("ctl00_ctl00_ContentPlaceHolder1_summaryContent_lblTotalUsage").text
DailyCost = driver.find_element_by_id("ctl00_ctl00_ContentPlaceHolder1_summaryContent_lblTotalCost").text
f.write("\"" + DailyUsage + "\",\"" + DailyCost + "\",")
driver.save_screenshot('HydroDailyUsage.png')
driver.find_element_by_xpath("//div[@id='ctl00_ctl00_TopNav1_tabMainNav']/div/ul/li[4]/a/span/span/span").click()
time.sleep(10)
MonthlyUsage = driver.find_element_by_id("ctl00_ctl00_ContentPlaceHolder1_summaryContent_lblTotalUsage").text
MonthlyCost = driver.find_element_by_id("ctl00_ctl00_ContentPlaceHolder1_summaryContent_lblTotalCost").text
f.write("\"" + MonthlyUsage + "\",\"" + MonthlyCost + "\"\n")
driver.save_screenshot('HydroMonthlyUsage.png')
f.close()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment