Created
February 25, 2024 06:51
-
-
Save nikhilweee/06191490fbbd1f511fd8a31d5f4965a7 to your computer and use it in GitHub Desktop.
Add time on workday using Selenium
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import time | |
import getpass | |
import argparse | |
import logging | |
from selenium import webdriver | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.common.keys import Keys | |
from selenium.webdriver.support.wait import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.chrome.service import Service | |
from selenium.webdriver.chrome.options import Options | |
logging.basicConfig(format="[%(levelname).1s]: %(message)s", level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
def setup_driver(): | |
options = Options() | |
username = getpass.getuser() | |
options.add_argument( | |
f"user-data-dir=/Users/{username}/Library/Application Support/Google/Chrome/Selenium" | |
) | |
options.add_experimental_option("detach", True) | |
service = Service() | |
driver = webdriver.Chrome(service=service, options=options) | |
action = webdriver.ActionChains(driver) | |
return driver, action | |
def login_to_workday(driver, args): | |
driver.get("https://wd5.myworkday.com/nyu/d/home.htmld") | |
if driver.title == "NYU Login": | |
logger.info("Redirected to Shibboleth") | |
elem = driver.find_element(by=By.NAME, value="j_username") | |
elem.send_keys(args.username) | |
elem = driver.find_element(by=By.NAME, value="j_password") | |
elem.send_keys(args.password) | |
elem = driver.find_element(by=By.NAME, value="_eventId_proceed") | |
elem.click() | |
logger.info("Logging in. Waiting for 2FA") | |
WebDriverWait(driver, args.timeout).until(EC.title_is("Workday nyu")) | |
logger.info("2FA Successful") | |
WebDriverWait(driver, args.timeout).until(EC.title_is("Home - Workday")) | |
logger.info("Logged into Workday") | |
def navigate_to_modal(driver, args, action): | |
if driver.title == "Home - Workday": | |
logger.info("Clicking Time Widget") | |
WebDriverWait(driver, args.timeout).until( | |
EC.presence_of_element_located( | |
(By.CSS_SELECTOR, "div[title='Time']") | |
) | |
).click() | |
logger.info(f"Clicking {args.week} Week") | |
WebDriverWait(driver, args.timeout).until( | |
EC.presence_of_element_located( | |
(By.CSS_SELECTOR, f"button[title^='{args.week} Week']") | |
) | |
).click() | |
logger.info("Clicking Actions") | |
WebDriverWait(driver, args.timeout).until( | |
EC.presence_of_element_located( | |
(By.CSS_SELECTOR, "button[title='Actions']") | |
) | |
).click() | |
logger.info("Clicking Enter Time") | |
enter_time = driver.find_element( | |
By.CSS_SELECTOR, "div[data-automation-label='Enter Time']" | |
) | |
WebDriverWait(driver, args.timeout).until(EC.element_to_be_clickable(enter_time)) | |
action.move_to_element(enter_time).click().perform() | |
logger.info("Opening Modal") | |
tabs = WebDriverWait(driver, args.timeout).until( | |
EC.presence_of_element_located( | |
(By.CSS_SELECTOR, "ul[data-automation-id='tabBar']") | |
) | |
) | |
logger.info("Modal Open") | |
tabs = tabs.find_elements(By.TAG_NAME, "li") | |
return tabs | |
def add_time(driver, args, action, tabs): | |
panels = driver.find_elements( | |
By.CSS_SELECTOR, "div[data-automation-id='panel']" | |
) | |
ok_button = driver.find_element( | |
By.CSS_SELECTOR, | |
"button[data-automation-id='wd-CommandButton_uic_okButton']", | |
) | |
assert len(tabs) == len(panels) == 7 | |
tabs_list = [tabs[x-1] for x in args.days] | |
panels_list = [panels[x-1] for x in args.days] | |
for tab, panel in zip(tabs_list, panels_list): | |
background = driver.find_element( | |
By.CSS_SELECTOR, "div[data-automation-id='numericText']" | |
) | |
day = tab.find_element(By.XPATH, "./div[2]/div") | |
WebDriverWait(driver, args.timeout).until(EC.element_to_be_clickable(tab)) | |
action.move_to_element(tab).click().perform() | |
action.move_to_element(background).pause(1).click().perform() | |
logger.info(f"Filling {day.text}") | |
WebDriverWait(driver, args.timeout).until( | |
attribute_is(tab, "data-automation-id", "selectedTab") | |
) | |
add_button = panel.find_element( | |
By.CSS_SELECTOR, | |
"button[data-automation-id=wd-timeEntryList-addButton]", | |
) | |
row = add_button.find_element(By.XPATH, "../div[1]") | |
labels = row.find_elements(By.TAG_NAME, "label") | |
label_in = next(filter(lambda x: x.text == "In", labels)) | |
input_in = label_in.find_element(By.XPATH, "../../div[2]/div/div/input") | |
# Add first time block | |
fill_row(driver, input_in, "10am", "12pm") | |
# add_button.click() | |
# row = WebDriverWait(driver, args.timeout).until( | |
# presence_of_element_relative(add_button, By.XPATH, "../div[2]") | |
# ) | |
# labels = row.find_elements(By.TAG_NAME, "label") | |
# label_in = next(filter(lambda x: x.text == "In", labels)) | |
# input_in = label_in.find_element(By.XPATH, "../../div[2]/div/div/input") | |
# # Add second time block | |
# fill_row(driver, input_in, "2pm", "5pm") | |
logger.info("Adding time complete. Please save changes.") | |
# logger.info("Saving changes") | |
# ok_button.click() | |
# WebDriverWait(driver, args.timeout).until( | |
# EC.presence_of_element_located( | |
# (By.CSS_SELECTOR, "button[title='Actions']") | |
# ) | |
# ) | |
# logger.info("Done") | |
# time.sleep(10) | |
# logger.info("Exiting") | |
# driver.quit() | |
def fill_row(driver, input_in, start_text, end_text): | |
logger.info(f"Adding {start_text} to {end_text}") | |
input_in.clear() | |
input_in.send_keys( | |
start_text, | |
Keys.TAB, | |
end_text, | |
Keys.TAB, | |
Keys.TAB, | |
Keys.SPACE, | |
) | |
input_pos = WebDriverWait(driver, 30).until( | |
EC.element_to_be_clickable( | |
(By.CSS_SELECTOR, "ul[data-automation-id='menuList']") | |
) | |
) | |
input_pos.send_keys( | |
Keys.DOWN, | |
Keys.DOWN, | |
Keys.DOWN, | |
Keys.ENTER, | |
) | |
def attribute_is(element, attribute_, value_): | |
def _predicate(_): | |
return value_ == element.get_attribute(attribute_) | |
return _predicate | |
def presence_of_element_relative(element, by_, value_): | |
def _predicate(_): | |
return element.find_element(by_, value_) | |
return _predicate | |
def main(args): | |
driver, action = setup_driver() | |
login_to_workday(driver, args) | |
tabs = navigate_to_modal(driver, args, action) | |
add_time(driver, args, action, tabs) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-u', '--username', required=True, help="NYU NetID") | |
parser.add_argument('-p', '--password', required=True, help="NYU Password") | |
parser.add_argument('-w', '--week', choices=['this', 'last'], default='this') | |
parser.add_argument('-d', '--days', nargs='+', type=int, default=[1, 2, 3, 4, 5]) | |
parser.add_argument('-t', '--timeout', type=int, default=30) | |
args = parser.parse_args() | |
args.week = args.week.capitalize() | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment