# ---------------------------------------------------------------------------------
# IMPORTS: Importing necessary modules from the Selenium and standard Python libraries
# ---------------------------------------------------------------------------------
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
# ---------------------------------------------------------------------------------
# INITIALIZATION FUNCTIONS (Corrected function and variable names)
# ---------------------------------------------------------------------------------
# initialize_service: Sets up the Chrome driver service.
def initialize_service():
# Corrected variable name: executable_path
executable_path = r"C:\Users\karan\Downloads\chromedriver-win64\chromedriver.exe"
# Creates a Service object for the Chrome browser.
service = Service(executable_path=executable_path)
return service
# open_website: Opens the specified URL and maximizes the window.
def open_website(driver):
# Corrected variable name: url. Removed '#' from the end of the URL.
url = r"file:///C:/Users/karan/Desktop/selenium_test_page.html"
# Navigates the browser to the local HTML file.
driver.get(url)
# Corrected method name: maximize_window()
driver.maximize_window()
# Pauses execution for 2 seconds to allow the page to fully load.
time.sleep(2)
# ---------------------------------------------------------------------------------
# TASK FUNCTIONS (Web Interactions) - Using IDs for stability
# ---------------------------------------------------------------------------------
def task1(driver):
print("Executing Task 1: Basic Inputs...")
# Click on the Radio Button "Option B"
driver.find_element(By.XPATH, "/html/body/div/div/section[1]/div/div[1]/div/div[2]/label[2]/input").click()
time.sleep(1)
# Click on the Checkbox
driver.find_element(By.ID, "myCheckbox").click()
time.sleep(1)
# Select "Option 2" from the Standard Dropdown
dropdown_element = driver.find_element(By.ID, "basic-dropdown")
select = Select(dropdown_element)
select.select_by_visible_text("Option 2")
time.sleep(1)
def task2(driver):
print("Executing Task 2: Delayed Alert Simulation...")
# Click to trigger the custom alert modal
driver.find_element(By.ID, "alert-trigger-btn").click()
# Wait for the custom alert modal to become visible
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.ID, "custom-alert-modal")))
# Input text into the alert's input field (simulating send_keys)
alert_input_element = driver.find_element(By.ID, "alert-input")
alert_input_element.clear()
alert_input_element.send_keys("Welcome to Selenium")
time.sleep(2)
# Accept the alert (simulates clicking 'OK')
driver.find_element(By.XPATH, "//button[text()='OK']").click()
time.sleep(2)
def task3(driver):
print("Executing Task 3: Multi-step Dropdown and Hover...")
# Click on "Redirect link page" to switch to the Task 3 section
driver.find_element(By.ID, "redirect-link-page").click()
time.sleep(1)
# Click on "Redirect to Country info page" (stays on the same section for this test page)
driver.find_element(By.ID, "redirect-country-link").click()
time.sleep(1)
# Select value "15" from the dropdown
dropdown_driver = driver.find_element(By.ID, "value-dropdown")
select = Select(dropdown_driver)
select.select_by_value("15")
time.sleep(1)
# Hover over the element to reveal the hidden option
hover_div_element = driver.find_element(By.ID, "hover-div")
actions = ActionChains(driver)
actions.move_to_element(hover_div_element).perform()
time.sleep(1)
# Select the revealed option by value
hidden_select = driver.find_element(By.ID, "hidden-option-select")
select_revealed = Select(hidden_select)
select_revealed.select_by_value("revealed")
time.sleep(1)
def task4(driver):
print("Executing Task 4: File Upload...")
# Click to navigate to the File Upload section
driver.find_element(By.ID, "file-upload-link").click()
time.sleep(2)
# Finds the file input element by its ID.
file_input_element = driver.find_element(By.ID, "uploadFile")
# IMPORTANT: The file must exist at this path for send_keys to work.
filepath_to_upload = r"C:\Users\karan\Desktop\selenium_test_page.html"
# Sends the file path to the input element (performs the upload)
file_input_element.send_keys(filepath_to_upload)
time.sleep(2)
def task5(driver):
print("Executing Task 5: Drag and Drop...")
# Click to navigate to the Drag and Drop section
driver.find_element(By.ID, "drag-drop-link").click()
time.sleep(2)
# Find the source element
source_driver = driver.find_element(By.ID, "dragFrom")
# Find the target drop zone element
target_driver = driver.find_element(By.ID, "dropZone")
# Performs the drag and drop action.
actions = ActionChains(driver)
actions.drag_and_drop(source_driver, target_driver).perform()
time.sleep(2)
# ---------------------------------------------------------------------------------
# MAIN EXECUTION (Centralized control flow)
# ---------------------------------------------------------------------------------
def main():
driver = None
try:
print("Starting Selenium Automation Script...")
# 1. Initialize the Chrome WebDriver service.
service = initialize_service()
# 2. Create the WebDriver instance.
driver = webdriver.Chrome(service=service)
# 3. Open the web page and maximize the window.
open_website(driver)
# 4. Execute the tasks sequentially.
task1(driver)
task2(driver)
task3(driver)
task4(driver)
task5(driver)
print("Automation tasks completed successfully.")
except Exception as e:
print(f"\n--- EXECUTION FAILED ---")
print(f"An error occurred: {e}")
print("\nPlease check the following common issues:")
print("1. Your Chrome browser version matches your 'chromedriver.exe' version.")
print("2. The path to 'chromedriver.exe' is exactly correct.")
print("3. The path to 'selenium_test_page.html' is exactly correct.")
finally:
# 5. Closes the browser window safely.
if driver:
print("Quitting driver.")
driver.quit()
if __name__ == "__main__":
main()