-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
67 additions
and
66 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,75 @@ | ||
from selenium import webdriver | ||
from selenium.webdriver.chrome.options import Options | ||
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.support.select import Select | ||
from webdriver_manager.chrome import ChromeDriverManager | ||
from webdriver_manager.core.os_manager import ChromeType | ||
from selenium.webdriver.chrome.options import Options | ||
from selenium.webdriver.chrome.service import Service as ChromiumService | ||
|
||
from playwright.sync_api import sync_playwright | ||
from datetime import datetime, timedelta | ||
import time | ||
|
||
def renew(username, password): | ||
try: | ||
with sync_playwright() as p: | ||
# Launch browser with necessary arguments | ||
browser = p.chromium.launch( | ||
headless=False, | ||
args=[ | ||
'--no-sandbox', | ||
'--disable-dev-shm-usage', | ||
'--remote-debugging-port=9222', | ||
'--disable-gpu', | ||
'--window-size=1920,1200', | ||
'--ignore-certificate-errors', | ||
'--disable-extensions' | ||
] | ||
) | ||
|
||
page = browser.new_page() | ||
|
||
# Navigate to login page | ||
page.goto('https://licenseportal.it.chula.ac.th/') | ||
|
||
# Fill in login credentials | ||
page.fill('#UserName', username) | ||
page.fill('#Password', password) | ||
|
||
# Click the login button | ||
page.click('button') | ||
|
||
# Wait for the 'Borrow' page to load and navigate | ||
page.wait_for_navigation() | ||
page.goto('https://licenseportal.it.chula.ac.th/Home/Borrow') | ||
|
||
# Wait for expiry date field to load | ||
page.wait_for_selector('#ExpiryDateStr') | ||
|
||
# Set the expiry date to 7 days from today | ||
week = datetime.now() + timedelta(days=7) | ||
page.fill('#ExpiryDateStr', week.strftime('%d/%m/%Y')) | ||
|
||
# Remove any unnecessary datepicker elements ('.dtp') which may block the Save button | ||
time.sleep(0.5) | ||
for dtp in page.query_selector_all('.dtp'): | ||
page.evaluate('(element) => element.remove()', dtp) | ||
|
||
# Select value for 'ProgramLicenseID' dropdown | ||
page.select_option('#ProgramLicenseID', value='5') | ||
|
||
# Wait for the Save button and click it | ||
page.wait_for_selector("button:text('Save')") | ||
page.click("button:text('Save')") | ||
|
||
return True | ||
|
||
except Exception as e: | ||
print(f"An error occurred: {e}") | ||
return False | ||
|
||
finally: | ||
browser.close() | ||
|
||
|
||
def renew(username, password): | ||
chrome_options = Options() | ||
options = [ | ||
"--no-sandbox", | ||
"--disable-dev-shm-usage", | ||
"--remote-debugging-port=9222", | ||
"--headless", | ||
"--disable-gpu", | ||
"--window-size=1920,1200", | ||
"--ignore-certificate-errors", | ||
"--disable-extensions" | ||
] | ||
for option in options: | ||
chrome_options.add_argument(option) | ||
driver = webdriver.Chrome(service=ChromiumService(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()), options=chrome_options) | ||
|
||
|
||
|
||
driver.get('https://licenseportal.it.chula.ac.th/') | ||
|
||
wait = WebDriverWait(driver, 10) | ||
wait.until(EC.presence_of_element_located((By.XPATH, '//input'))) | ||
|
||
username_input = driver.find_element(By.ID, 'UserName') | ||
username_input.send_keys(username) | ||
|
||
password_input = driver.find_element(By.ID, 'Password') | ||
password_input.send_keys(password) | ||
|
||
signin_button = driver.find_element(By.XPATH, '//button') | ||
signin_button.click() | ||
|
||
driver.get('https://licenseportal.it.chula.ac.th/Home/Borrow') | ||
|
||
|
||
|
||
wait.until(EC.presence_of_all_elements_located((By.ID, 'ExpiryDateStr'))) | ||
|
||
expiry_date_input = driver.find_element(By.ID, 'ExpiryDateStr') | ||
week = datetime.now() + timedelta(days=7) | ||
driver.execute_script("arguments[0].value = arguments[1];", expiry_date_input, week.strftime('%d/%m/%Y')) | ||
|
||
|
||
select_element = driver.find_element(By.ID, 'ProgramLicenseID') | ||
select = Select(select_element) | ||
select.select_by_value('5') | ||
|
||
save_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Save']"))) | ||
driver.execute_script("arguments[0].scrollIntoView(true);", save_button) | ||
save_button.click() | ||
|
||
driver.quit() | ||
|
||
return True | ||
|
||
import os | ||
USERNAME = os.environ['USERNAME'] | ||
PASSWORD = os.environ['PASSWORD'] | ||
renew(USERNAME, PASSWORD) | ||
|
||
if renew(USERNAME, PASSWORD): | ||
print("Renewal process completed successfully.") | ||
else: | ||
print("Renewal process failed.") |