Skip to content

Instantly share code, notes, and snippets.

@davidair
Created April 11, 2021 05:03
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 davidair/c002d16355eb121916238c27473528b1 to your computer and use it in GitHub Desktop.
Save davidair/c002d16355eb121916238c27473528b1 to your computer and use it in GitHub Desktop.
Selenium Downloader for TD Canada Trust statements (technique described in https://airtdave.medium.com/exploring-web-automation-with-dynamic-code-f727b84fb7c0)
# Copyright 2021 Google LLC.
# SPDX-License-Identifier: Apache-2.0
if not ('main_window_handle' in vars() or 'main_window_handle' in globals()):
print('main_window_handle is not defined')
main_window_handle = driver.current_window_handle
driver.switch_to.window(main_window_handle)
frame = driver.find_element_by_css_selector('frame')
driver.switch_to.frame(frame)
for anchor in driver.find_elements_by_xpath("//a[@class='table td-link-newwindow']"):
print(anchor.text)
anchor.click()
time.sleep(1)
for handle in driver.window_handles:
if handle == main_window_handle:
continue
driver.switch_to.window(handle)
driver.close()
driver.switch_to.window(main_window_handle)
# Copyright 2021 Google LLC.
# SPDX-License-Identifier: Apache-2.0
import pathlib
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
last_modified_time = 0
download_directory = str(pathlib.Path.home().joinpath('Downloads', 'statements'))
chrome_options = Options()
chrome_options.add_experimental_option('prefs', {
"download.default_directory": download_directory,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True
}
)
driver = webdriver.Chrome(options = chrome_options)
driver.get('https://www.td.com/ca/en/personal-banking/')
print('Waiting for 5 seconds...')
time.sleep(5)
print('Finding login button')
element = driver.find_element_by_xpath("//button[@class='td-button td-button-secondary td-button-large td-copy-nowrap loginout']")
element.click()
while True:
time.sleep(100 / 1000)
fname = pathlib.Path('dyncode_td.py')
if not fname.exists():
continue
modified_time = fname.stat().st_mtime
if last_modified_time != modified_time:
print('dyncode changed, evaluating...')
last_modified_time = modified_time
with open('dyncode_td.py') as f:
try:
exec(f.read())
except Exception as e:
print('Error evaluation code: %s' % e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment