-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattendance_bulletin.py
141 lines (113 loc) · 4.32 KB
/
attendance_bulletin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from dotenv import load_dotenv
import os
import time
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import StaleElementReferenceException, TimeoutException
from selenium.webdriver import ActionChains
from datetime import datetime
load_dotenv()
ASPEN_URL=os.getenv("ASPEN_URL")
LOGIN_ID = os.getenv("LOGIN_ID")
PASSWORD = os.getenv("PASSWORD")
download_dir = os.path.join(os.getcwd(), "downloads/bulletin") # Downloads to the "downloads" folder in your project
if not os.path.exists(download_dir):
os.makedirs(download_dir)
chrome_options = webdriver.ChromeOptions()
prefs = {
"download.default_directory": download_dir, # Set the download directory
"download.prompt_for_download": False, # Disable download prompts
"plugins.always_open_pdf_externally": True # Download PDFs instead of opening in the browser
}
chrome_options.add_experimental_option("prefs", prefs)
# Instantiate the driver with the defined options
driver = webdriver.Chrome(options=chrome_options)
driver.get(ASPEN_URL)
print(driver.current_url)
driver.implicitly_wait(5)
# Login
username = driver.find_element(By.ID, "username")
password = driver.find_element(By.ID, "password")
submit = driver.find_element(By.XPATH, '/html/body/go-root/go-login/go-login-container/div/div/div/go-default-login/form/div[4]/div/button')
username.send_keys(LOGIN_ID)
password.send_keys(PASSWORD)
submit.click()
# Wait for the page to load
#test
driver.implicitly_wait(10)
print(driver.current_url)
print("this worked")
# find attendance tab
attendance_tab = driver.find_element(By.LINK_TEXT, "Attendance")
attendance_tab.click()
# Wait for the page to load
driver.implicitly_wait(5)
# find the reports tab
reports_tab = driver.find_element(By.ID, "reportsMenu")
# click the reports tab
reports_tab.click()
# Wait for the page to load
driver.implicitly_wait(2)
# access submenus
# find the attendance tab
reports_menusub1 = driver.find_element(By.ID, "reportsMenuSub1")
reports_menusub1.click()
# ATTENDANCE BULLET REPORT
consecutive_absence_report = driver.find_element(By.XPATH, "/html/body/form/table/tbody/tr[2]/td/div/table[2]/tbody/tr[1]/td[2]/table[1]/tbody/tr/td[3]/div[2]/table/tbody/tr[7]/td[3]/div/table/tbody/tr[1]/td[2]")
consecutive_absence_report.click()
# Wait for the page to load
driver.implicitly_wait(5)
'''
==================== MODAL WINDOW ====================
'''
# Wait for the new window to appear
WebDriverWait(driver, 10).until(lambda d: len(d.window_handles) > 1)
# Get all window handles
window_handles = driver.window_handles
# Switch to the new window (assuming it’s the second one)
driver.switch_to.window(window_handles[-1])
# Wait for the modal to be visible
WebDriverWait(driver, 5).until(
EC.visibility_of_element_located((By.ID, 'popupWindow'))
)
# wait for it to be clickable
WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH, '/html/body/table'))
)
# this opens a modal window with options
# element of table containing options
table = driver.find_element(By.CLASS_NAME, "detailContainer")
# start date
# DEFAULT DATE IS TODAY, SO NO NEED TO CHANGE
'''
==================== OPTIONS ====================
can do TARDIES, etc. with CONNECTORS like AND & OR
can also output as csv rather than html or pdf
right now all this is set by default to all students
'''
'''
==================== SUBMIT ====================
'''
# run_button = driver.find_element(By.ID, "okButton")
# wait for it to be clickable
# print(driver.find_element(By.TAG_NAME, "button").find_element(By.CLASS_NAME, "button-text").outerHTML)
ok_button = WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, '//*[@tabindex="99"]'))
)
ok_button.click()
time.sleep(40) # Increase the wait time if the PDF takes longer to download
pdf_url = driver.current_url
# Check if URL is pointing to a PDF file
if pdf_url.endswith(".pdf"):
response = requests.get(pdf_url)
if response.status_code == 200:
# Save the PDF to a file
with open("output.pdf", "wb") as f:
f.write(response.content)
print("PDF saved as output.pdf")
print(f"PDF should be downloaded to: {download_dir}")
driver.quit()