A showcase of two distinct automation paradigms: Complex User Interaction for government portals and High-Throughput Parallel Processing for bulk data validation.
This script automates the complex login workflow for the Skill India Digital portal. It handles multi-step user choices, dynamic element selection from a list, secure credential entry, new tab management, and direct navigation to specific internal pages.
Uses a CSS selector to gather all possible login buttons and then programmatically selects the 16th one, a robust method for handling dynamically generated UIs.
After clicking "Connect," the script waits for a second browser window to open and then switches the WebDriver's active control to the new tab.
Utilizes a combination of XPath, ID, and CSS selectors with `WebDriverWait` to reliably interact with elements like input fields, links, and SVG icons.
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 import time driver = webdriver.Chrome() try: # --- Phase 1: Initial Navigation & User Role Selection --- driver.get("https://wwww.ForExampleUse.in/home") # Click the main "Login" button in the header login_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/app-root/app-container/app-discovery-header/header/div/div[1]/div/div[2]/ul/li[6]/button"))) login_button.click() # On the role selection screen, click the "Partner" image button partner_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//img[@alt='Partner']"))) partner_button.click() # --- Phase 2: Dynamic Button Selection --- # Wait for all login buttons with class 'registers-btn' to be present login_buttons = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "button.registers-btn"))) if len(login_buttons) >= 16: target_button = login_buttons[15] # Select the 16th button (index 15) # Scroll the button into view and click it driver.execute_script("arguments[0].scrollIntoView(true);", target_button) time.sleep(1) target_button.click() else: raise Exception("Could not find the 16th login button.") # --- Phase 3: Authentication & Tab Switching --- # Enter username and password into their respective fields WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[placeholder='User Name']"))).send_keys("TP052135") WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[placeholder='Password']"))).send_keys("Bruno@2018") # Click the "Connect" button to log in WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Connect')]"))).click() # Click the "Ok" button on the confirmation popup WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn-primary-style2"))).click() # Wait for a new tab to open, then switch to it WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2)) driver.switch_to.window(driver.window_handles[1]) # --- Phase 4: Deep Linking and Final Action --- # Navigate directly to a specific batch details page driver.get("https://www.ForExampleUse.in/admin-profile/trainingpartner/view-batch-details/68904/TC296141/3269675?schemeReferenceId=Scheme_1012") # Click the "Enrolled Candidates" tab to view the list WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Enrolled Candidates']"))).click() # Find the first candidate's action menu (SVG icon) and click it first_svg_element = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//svg[contains(@stroke, '#DF6E12')]")))[0] driver.execute_script("arguments[0].click();", first_svg_element) # From the dropdown, click "Update Profile Photo" update_link = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Update Profile Photo']"))) update_link.click() # Finally, click the file input to trigger the upload dialog WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "customFile"))).click() except Exception as e: print(f"An error occurred: {e}") finally: input("Script finished. Press Enter to close browser...")
A high-performance script designed to validate over 150 barcode records against a public database. It simulates parallel processing by launching multiple, independent Chrome instances, each handling a subset of the data. This drastically reduces total runtime and generates a comprehensive Excel status report.
The script instantiates six separate WebDriver objects. Each driver works on a distinct slice of the URL list, allowing six records to be validated simultaneously.
A custom function scans the page for a specific error label (label for='No_Record_Present'). This is more reliable than checking for generic error pages.
All results (Success, No Record Present, Error) are compiled into a list of dictionaries, which is then converted into a Pandas DataFrame and saved as an Excel file.
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 time import sleep import pandas as pd # Complete list of barcode numbers for validation barcodes = [ "12872210204001971049", "12862207014004378758", "12872412194004257577", "12872410294003977150", "12872209024001380463", "12672306220255273979", "12862503124004948699", "12872409274003632642", "12862301284007591810", "12862305274003782365", # ... (and 150+ more barcodes) ... "12862410104003791939", "12862412214004281044" ] # Anonymized base URL for the service endpoint base_url = "https://www.ForExampleUse.in/Views/SearchBarCode/DisplayBarCodeData?deptName=Revenue&serviceId=1286&barCode=" urls = [base_url + barcode for barcode in barcodes] data = [] # Global list to store results from all drivers def check_for_no_record_present_label(driver): try: return driver.find_element(By.XPATH, "//label[@for='No_Record_Present']").is_displayed() except Exception: return False def process_urls(driver, urls_to_process): for url in urls_to_process: driver.get(url) try: # Wait for the page body to be loaded before checking content WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.TAG_NAME, "body"))) if check_for_no_record_present_label(driver): data.append({"URL": url, "Status": "No Record Present"}) else: data.append({"URL": url, "Status": "Success"}) except Exception as e: data.append({"URL": url, "Status": f"Error: {str(e)}"}) sleep(2) # Polite delay to avoid overwhelming the server # --- Parallel Execution Setup --- driver1 = webdriver.Chrome() driver2 = webdriver.Chrome() driver3 = webdriver.Chrome() driver4 = webdriver.Chrome() driver5 = webdriver.Chrome() driver6 = webdriver.Chrome() # Divide the URLs into 6 batches and process them in parallel process_urls(driver1, urls[0:30]) process_urls(driver2, urls[30:60]) process_urls(driver3, urls[60:90]) process_urls(driver4, urls[90:120]) process_urls(driver5, urls[120:150]) process_urls(driver6, urls[150:]) # Process the remainder # --- Reporting and Cleanup --- df = pd.DataFrame(data) df.to_excel("link_status.xlsx", index=False) for drv in [driver1, driver2, driver3, driver4, driver5, driver6]: drv.quit() print("Process completed. Results saved to 'link_status.xlsx'.")
These scripts are provided for educational purposes to demonstrate advanced Selenium techniques. The target URLs and credentials in these examples have been anonymized. Always ensure you have authorization before running automated bots on government or third-party portals.