#!/usr/bin/env python3 import time import os import sys from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.common.exceptions import NoSuchElementException if "help" in sys.argv or "h" in sys.argv or "-h" in sys.argv or "--help" in sys.argv: print(f"""{sys.argv[0]} [jt|johtajatiedote] Hakee kuksasta sähköpostit joko perhe- tai johtajatiedotteeseen. Oletuksena perhetiedote.""") sys.exit(0) def find_element(by, something): """ Try driver.find_element until the element is found. Make sure the element eventually appears or this can stall for ever. """ try: return driver.find_element(by, something) except NoSuchElementException: time.sleep(1) return find_element(by, something) driver = webdriver.Firefox() emails = [] driver.get("https://kuksa.partio.fi") # This is my (slaesvuo) way to implement unattended login and probably will not # work for you as is #passwd = os.popen("pass show web/id.partio.fi | head -n 1").read() #username_field = find_element(By.ID, 'username') #username_field.send_keys("saku-laesvuori" + Keys.TAB + passwd + Keys.ENTER) find_element(By.ID, 'sivu_poiminta').click() if "johtajatiedote" in sys.argv or "jt" in sys.argv: find_element(By.ID, 'cphContent_IYleiset').click() birthdate_upper_bound_input = find_element(By.ID, 'cphContent_TAHSyntymaaika2_txtPvm') birthdate_upper_bound_input.send_keys("31.12.2008" + Keys.ENTER) else: find_element(By.ID, 'cphContent_iJasenyys').click() find_element(By.ID, 'cphContent_chkMyosJasenenHuoltajat').click() find_element(By.ID, 'cphContent_btnPoimi').click() while True: find_element(By.CSS_SELECTOR, 'tbody') # Wait for the table to be loaded emails += [x.text for x in driver.find_elements(By.CSS_SELECTOR, 'tr > td:nth-child(5)')] nextLink = find_element(By.XPATH, "//span[@id='cphContent_dpPoiminta']/a[text()='>']") if nextLink.get_attribute("class") == "aspNetDisabled": break nextLink.click() for email in {x.lower() for x in emails}: if email != "": print(email) driver.close()