Alyssa Smith revised this gist . Go to revision
1 file changed, 8 insertions, 12 deletions
afterpay-fetch-data.py
| @@ -2,32 +2,28 @@ from selenium import webdriver | |||
| 2 | 2 | from selenium.webdriver.common.by import By | |
| 3 | 3 | from selenium.webdriver.support.ui import WebDriverWait | |
| 4 | 4 | from selenium.webdriver.support import expected_conditions as EC | |
| 5 | - | from selenium.webdriver.chrome.options import Options | |
| 5 | + | from subprocess import Popen, PIPE | |
| 6 | 6 | from os import environ | |
| 7 | 7 | ||
| 8 | 8 | try: | |
| 9 | - | driver = webdriver.Chrome("Downloads\\chromedriver.exe") | |
| 10 | - | ||
| 9 | + | driver = webdriver.Chrome(environ.get("CHROMEDRIVER_PATH", "chromedriver.exe")) | |
| 11 | 10 | driver.get("https://portal.afterpay.com/au/login-email") | |
| 12 | 11 | _wait = WebDriverWait(driver, 10) | |
| 12 | + | wait = lambda selector: _wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, selector))) | |
| 13 | 13 | ||
| 14 | - | def wait(selector): | |
| 15 | - | return _wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, selector))) | |
| 16 | - | ||
| 17 | - | def wait_then_input(selector, inp): | |
| 18 | - | return _wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, selector))).send_keys(inp) | |
| 19 | - | ||
| 20 | - | wait_then_input("input[automation_id='input-email']", environ["AFTERPAY_EMAIL"]) | |
| 14 | + | # Session init | |
| 15 | + | wait("input[automation_id='input-email']").send_keys(environ["AFTERPAY_EMAIL"]) | |
| 21 | 16 | driver.find_elements_by_css_selector("button[automation_id='button-submit']")[0].click() | |
| 22 | 17 | ||
| 23 | - | wait_then_input("input[automation_id='input-password']", environ["AFTERPAY_PASSWORD"]) | |
| 18 | + | wait("input[automation_id='input-password']").send_keys(environ["AFTERPAY_PASSWORD"]) | |
| 24 | 19 | driver.find_elements_by_class_name("checkbox__tick")[0].click() | |
| 25 | 20 | driver.find_elements_by_css_selector("button[automation_id='button-submit']")[0].click() | |
| 26 | 21 | wait(".orders-banner__panel") | |
| 27 | 22 | ||
| 23 | + | # Data fetch | |
| 28 | 24 | driver.get("https://portalapi.afterpay.com/portal/consumers/paymentschedule/due?ascending=true&limit=100&offset=0&orderBy=dueDate") | |
| 29 | 25 | ||
| 30 | - | with open("Downloads\\afterpay-due.json", "w") as f: | |
| 26 | + | with open("afterpay-due.json", "w") as f: | |
| 31 | 27 | f.write(driver.find_element_by_tag_name('pre').text) | |
| 32 | 28 | finally: | |
| 33 | 29 | driver.quit() | |
Alyssa Smith revised this gist . Go to revision
1 file changed, 0 insertions, 0 deletions
gistfile1.txt renamed to afterpay-fetch-data.py
File renamed without changes
Alyssa Smith revised this gist . Go to revision
1 file changed, 33 insertions
gistfile1.txt(file created)
| @@ -0,0 +1,33 @@ | |||
| 1 | + | from selenium import webdriver | |
| 2 | + | from selenium.webdriver.common.by import By | |
| 3 | + | from selenium.webdriver.support.ui import WebDriverWait | |
| 4 | + | from selenium.webdriver.support import expected_conditions as EC | |
| 5 | + | from selenium.webdriver.chrome.options import Options | |
| 6 | + | from os import environ | |
| 7 | + | ||
| 8 | + | try: | |
| 9 | + | driver = webdriver.Chrome("Downloads\\chromedriver.exe") | |
| 10 | + | ||
| 11 | + | driver.get("https://portal.afterpay.com/au/login-email") | |
| 12 | + | _wait = WebDriverWait(driver, 10) | |
| 13 | + | ||
| 14 | + | def wait(selector): | |
| 15 | + | return _wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, selector))) | |
| 16 | + | ||
| 17 | + | def wait_then_input(selector, inp): | |
| 18 | + | return _wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, selector))).send_keys(inp) | |
| 19 | + | ||
| 20 | + | wait_then_input("input[automation_id='input-email']", environ["AFTERPAY_EMAIL"]) | |
| 21 | + | driver.find_elements_by_css_selector("button[automation_id='button-submit']")[0].click() | |
| 22 | + | ||
| 23 | + | wait_then_input("input[automation_id='input-password']", environ["AFTERPAY_PASSWORD"]) | |
| 24 | + | driver.find_elements_by_class_name("checkbox__tick")[0].click() | |
| 25 | + | driver.find_elements_by_css_selector("button[automation_id='button-submit']")[0].click() | |
| 26 | + | wait(".orders-banner__panel") | |
| 27 | + | ||
| 28 | + | driver.get("https://portalapi.afterpay.com/portal/consumers/paymentschedule/due?ascending=true&limit=100&offset=0&orderBy=dueDate") | |
| 29 | + | ||
| 30 | + | with open("Downloads\\afterpay-due.json", "w") as f: | |
| 31 | + | f.write(driver.find_element_by_tag_name('pre').text) | |
| 32 | + | finally: | |
| 33 | + | driver.quit() | |