Spaces:
Sleeping
Sleeping
from selenium import webdriver | |
from selenium.webdriver.chrome.options import Options | |
from selenium.webdriver.chrome.service import Service | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
import chromedriver_autoinstaller # pip install chromedriver-autoinstaller selenium | |
import time | |
start_time = time.time() | |
# Auto-install matching ChromeDriver | |
chromedriver_autoinstaller.install() | |
def fetch_new_cookies(): | |
# Headless Chrome options | |
chrome_options = Options() | |
chrome_options.add_argument("--headless=new") | |
# Launch browser | |
driver = webdriver.Chrome(options=chrome_options) | |
try: | |
# Go to page | |
driver.get("https://epay.unionbankofindia.co.in/kvchallan/default.aspx") | |
# Wait for CAPTCHA to load | |
wait = WebDriverWait(driver, 5) | |
captcha_text = wait.until(EC.presence_of_element_located((By.ID, "lblCaptcha"))).text | |
# Fill in the form | |
driver.find_element(By.ID, "txtStudent").send_keys("091438109008704") | |
driver.find_element(By.ID, "txtDateofBirth").send_keys("09/05/2002") | |
driver.find_element(By.ID, "txtCaptcha").send_keys(captcha_text) | |
# Click the image-style login button | |
driver.find_element(By.ID, "imgLogin").click() | |
# Extract ASP.NET_SessionId cookie | |
session_cookie = next((c for c in driver.get_cookies() if c['name'] == "ASP.NET_SessionId"), None) | |
if session_cookie: | |
# Print the cookie value | |
print("Session Cookie:", session_cookie['value']) | |
return session_cookie['value'] | |
else: | |
return fetch_new_cookies() | |
except Exception as e: | |
print("Error occurred:", e) | |
return None | |
finally: | |
driver.quit() | |