Spaces:
Sleeping
Sleeping
File size: 1,845 Bytes
78f015b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
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()
|