Created
February 8, 2025 14:05
-
-
Save ardzz/71f7d138577ec700eb4549ee5bb8dfbd to your computer and use it in GitHub Desktop.
This file contains hidden or 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 random | |
from rich.prompt import Prompt | |
from rich.table import Table | |
from selenium import webdriver | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support.ui import Select | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
from rich import console, table | |
console = console.Console() | |
USERNAME = "x.xx.xx.x.xx" | |
PASSWORD = "xxxxxx" | |
LOGIN_URL = "https://bakpk.polines.ac.id/login/mahasiswa" | |
QUESTIONNAIRE_URL = "https://bakpk.polines.ac.id/kuisioner/dosen" | |
driver = webdriver.Chrome() | |
def login(): | |
driver.get(LOGIN_URL) | |
username_field = driver.find_element(By.ID, "username") | |
username_field.send_keys(USERNAME) | |
password_field = driver.find_element(By.ID, "userpassword") | |
password_field.send_keys(PASSWORD) | |
try: | |
login_button = driver.find_element(By.XPATH, | |
"/html/body/div/div/div/div/div/div/div/div[2]/div[2]/form/div[4]/button") | |
login_button.click() | |
except: | |
console.print("Error: Login button not found") | |
exit() | |
if driver.current_url == LOGIN_URL: | |
console.print("Error: Login failed") | |
exit() | |
else: | |
console.print("Login success") | |
def get_questionnaire_table(): | |
driver.get(QUESTIONNAIRE_URL) | |
WebDriverWait(driver, 10).until( | |
EC.presence_of_element_located((By.CSS_SELECTOR, ".table-responsive")) | |
) | |
table = driver.find_element(By.CSS_SELECTOR, '.table-responsive') | |
rows = [] | |
# #layout-wrapper > div.main-content > div > div > div.card.card-body > div.table-responsive > table > tbody | |
for idx, tr in enumerate(table.find_elements(By.CSS_SELECTOR, "tbody tr.align-middle")): | |
cells = tr.find_elements(By.TAG_NAME, "td") | |
status = cells[5].text.strip() | |
rows.append({ | |
"index": idx + 1, | |
"subject": cells[3].text.strip(), | |
"status": status, | |
"button": cells[6].find_element(By.TAG_NAME, "button") if status == "Belum Terisi" else None | |
}) | |
return rows | |
login() | |
rows = get_questionnaire_table() | |
rich_table = Table(show_header=True) | |
for header in ["No", "Subject", "Status", "Action"]: | |
rich_table.add_column(header) | |
for row in rows: | |
rich_table.add_row( | |
str(row["index"]), | |
row["subject"], | |
row["status"], | |
"[green]Available[/green]" if row["status"] == "Belum Terisi" else "[red]Completed[/red]" | |
) | |
console.print(rich_table) | |
while True: | |
try: | |
choose_class = int(Prompt.ask("Pilih matkul (0 to exit): ", choices=[str(i) for i in range(0, len(rows) + 1)])) | |
if choose_class == 0: | |
break | |
selected = rows[choose_class - 1] | |
if selected["status"] == "Terisi": | |
console.print("[bold red]Error: Kuisioner sudah diisi[/bold red]") | |
continue | |
modal_id = selected["button"].get_attribute("data-bs-target") | |
selected["button"].click() | |
# #isiKuisionerModal46002 > div > div | |
modal_css = f"{modal_id} > div > div" | |
WebDriverWait(driver, 5).until( | |
EC.visibility_of_element_located((By.CSS_SELECTOR, modal_css)) | |
) | |
modal = driver.find_element(By.CSS_SELECTOR, modal_css) | |
questions = modal.find_elements(By.CSS_SELECTOR, "tbody tr") | |
selects = modal.find_elements(By.CSS_SELECTOR, "select.form-select") | |
console.print("\n[bold yellow]=== Questions ===") | |
number_question = 1 | |
for idx, question in enumerate(questions, 1): | |
if "#" not in question.text: | |
question_text = question.find_elements(By.TAG_NAME, "td")[1].text | |
cleaned_text = question_text.replace("\n", " ").strip() | |
console.print(f"[bold]{number_question}. {cleaned_text}[/bold]") | |
number_question += 1 | |
for select in selects: | |
Select(select).select_by_value("5") # Select "Sangat Baik" | |
modal.find_element(By.XPATH, ".//button[@type='submit']").click() | |
WebDriverWait(driver, 10).until( | |
EC.invisibility_of_element_located((By.CSS_SELECTOR, modal_css)) | |
) | |
rows = get_questionnaire_table() | |
console.print("[bold green]Kuisioner berhasil disubmit![/bold green]") | |
except Exception as e: | |
console.print_exception() | |
driver.refresh() | |
rows = get_questionnaire_table() | |
driver.quit() | |
console.print("[bold red]Exiting...[/bold red]") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
don't forget to install the dependencies
pip install selenium rich