Last active
February 15, 2024 09:39
-
-
Save EricDriussi/fc430714aae306d5128b408706057946 to your computer and use it in GitHub Desktop.
script to clock in and out from factorial.hr with more than one account
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 sys | |
from selenium import webdriver | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.chrome.options import Options | |
email_pwd = { | |
'[email protected]': 'micaballocaminapalante', | |
'[email protected]': 'estanolahackean' | |
} | |
sign_in_url = "https://api.factorialhr.com/es/users/sign_in" | |
dashboard_url = "https://app.factorialhr.com/dashboard" | |
clock_in_text = "Entrada" | |
clock_out_text = "Detener" | |
if "-o" in sys.argv: # -o for overtime | |
clock_out_confirm_text = "Confirmar tiempo registrado" | |
else: | |
clock_out_confirm_text = "Confirmar estimación" | |
timeout = 4 # try increasing if script doesn't work | |
browser = None | |
chrome_options = Options() | |
chrome_options.add_argument("--headless") | |
chrome_options.add_argument("--disable-gpu") | |
def login(browser, email, password): | |
print("Login in for:", email) | |
# Wait for email field to be present | |
email_field = WebDriverWait(browser, timeout).until( | |
EC.presence_of_element_located((By.NAME, "user[email]")) | |
) | |
# Wait for password field to be present | |
password_field = WebDriverWait(browser, timeout).until( | |
EC.presence_of_element_located((By.NAME, "user[password]")) | |
) | |
# Fill in the email and password fields | |
email_field.send_keys(email) | |
password_field.send_keys(password) | |
# Submit login form | |
login_button = WebDriverWait(browser, timeout).until( | |
EC.element_to_be_clickable((By.CLASS_NAME, "js-submit")) | |
) | |
login_button.click() | |
print("Success!") | |
def clock_in_or_out(browser): | |
clock_out_button = find_clock_out_button(browser) | |
if clock_out_button: | |
clock_out(browser, clock_out_button) | |
return | |
clock_in_button = find_clock_in_button(browser) | |
if clock_in_button: | |
clock_in(browser, clock_in_button) | |
return | |
def clock_out(browser, clock_out_button): | |
print("Clocking out...") | |
try: | |
clock_out_button.click() | |
# Wait for opposite button, confirm no hang | |
clock_in_button_rendered = find_clock_in_button(browser) | |
if clock_in_button_rendered: | |
print("Success!") | |
return | |
# If not, confirmation modal appeared (time exceeded) | |
print("You did overtime! Confirming time based on -o flag.") | |
confirm_button = find_button(browser, clock_out_confirm_text) | |
confirm_button.click() | |
# Wait for opposite button, confirm no hang | |
find_clock_in_button(browser) | |
print("Success!") | |
except Exception as e: | |
print("Error clocking out:", e) | |
def clock_in(browser, clock_in_button): | |
print("Clocking in...") | |
try: | |
clock_in_button.click() | |
# Wait for opposite button, confirm no hang | |
find_clock_out_button(browser) | |
print("Success!") | |
except Exception as e: | |
print("Error clocking in:", e) | |
def find_button(driver, button_text): | |
try: | |
return WebDriverWait(driver, timeout).until( | |
EC.element_to_be_clickable( | |
(By.XPATH, f"//*[contains(text(), '{button_text}')]")) | |
) | |
except BaseException: | |
return None | |
def find_clock_in_button(driver): | |
return find_button(driver, clock_in_text) | |
def find_clock_out_button(driver): | |
return find_button(driver, clock_out_text) | |
for email, password in email_pwd.items(): | |
try: | |
browser = webdriver.Chrome(options=chrome_options) | |
browser.get(sign_in_url) | |
login(browser, email, password) | |
browser.get(dashboard_url) | |
clock_in_or_out(browser) | |
finally: | |
browser.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment