Course

Selenium with
Python

Master browser automation with Selenium. Practice syntax, locators, and interactions in a safe, static environment.

Selenium Setup & Basics

Learn how to initialize a WebDriver and navigate to web pages. This sets the foundation for browser automation.

# Import WebDriver
from selenium import webdriver

# Initialize Chrome Driver
driver = webdriver.Chrome()

# Navigate to a URL
driver.get("https://google.com")

# Get the current URL
current_url = driver.current_url
print(current_url)

# Get the page title
title = driver.title
print(title)

# Close the browser
driver.quit()

Practice Tasks (Static Validation)

  • Initialize a webdriver.Chrome() instance named driver.
  • Navigate to "https://example.com" using driver.get().
  • Store the page title in a variable named page_title.
  • Close the browser using driver.quit().

Locators

Locators are used to find elements on a web page. Selenium provides several strategies like ID, Name, Class Name, CSS Selector, and XPath.

from selenium.webdriver.common.by import By

# Find by ID
element = driver.find_element(By.ID, "submit-button")

# Find by Name
user_field = driver.find_element(By.NAME, "username")

# Find by CSS Selector
login_btn = driver.find_element(By.CSS_SELECTOR, ".btn-primary")

# Find by XPath
header = driver.find_element(By.XPATH, "//h1[@class='title']")

# Find Multiple Elements
links = driver.find_elements(By.TAG_NAME, "a")

Practice Tasks

  • Find an element by ID "login-form".
  • Find an element by Name "email_address".
  • Select an element with class "submit-btn" using CSS Selector.
  • Find an element using XPath //div[@id='content'].

Interactions

Once you've found an element, you need to interact with it. Common actions include clicking buttons and typing text into input fields.

# Click a button
submit_btn = driver.find_element(By.ID, "submit")
submit_btn.click()

# Type text into an input field
search_box = driver.find_element(By.NAME, "q")
search_box.send_keys("Selenium Python")

# Clear an input field
search_box.clear()

# Submit a form (alternative to clicking submit button)
search_box.submit()

Practice Tasks

  • Click on an element named login_button.
  • Type "user@example.com" into the element named email_field.
  • Clear the text from the element named username_input.

Waits

Web pages take time to load completely. Using waits ensures your script doesn't fail by trying to interact with an element that isn't ready yet.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Implicit Wait (Global wait for all elements)
driver.implicitly_wait(10) # Seconds

# Explicit Wait (Wait for a specific condition)
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, "submit")))

print("Element is ready!")
element.click()

Practice Tasks

  • Set an implicit wait of 5 seconds.
  • Create a WebDriverWait instance named wait with a 10-second timeout.
  • Use EC.presence_of_element_located to wait for ID "header".

Dropdowns & Alerts

Learn how to handle HTML select dropdowns and JavaScript alerts.

from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By

# Handling Dropdowns
dropdown_element = driver.find_element(By.ID, "colors")
select = Select(dropdown_element)

# Select by visible text
select.select_by_visible_text("Blue")

# Select by value attribute
select.select_by_value("blue_val")

# Select by index (0-based)
select.select_by_index(1)

# Handling Alerts
alert_btn = driver.find_element(By.ID, "alert_btn")
alert_btn.click()

# Switch to alert
alert = driver.switch_to.alert
print(alert.text) # Get text
alert.accept() # Click OK
# alert.dismiss() # Click Cancel
# alert.send_keys("Input") # Type in prompt

Practice Tasks

  • Initialize a Select object using element with ID "dates".
  • Select the option "Year" by visible text.
  • Switch to the current alert and accept it.

Action Chains

ActionChains are used for automating complex user interactions like mouse hover, double click, right click, and drag & drop.

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

actions = ActionChains(driver)

element = driver.find_element(By.ID, "menu")

# Hover over element
actions.move_to_element(element).perform()

# Double Click
btn = driver.find_element(By.ID, "dbl_btn")
actions.double_click(btn).perform()

# Right Click (Context Click)
actions.context_click(element).perform()

# Drag and Drop
source = driver.find_element(By.ID, "drag")
target = driver.find_element(By.ID, "drop")
actions.drag_and_drop(source, target).perform()

# Method Chaining
actions.move_to_element(element).click().perform()

Practice Tasks

  • Create an ActionChains instance named actions.
  • Perform a double click on element my_btn.
  • Move to element menu_item and perform the action.

Windows & Frames

Learn how to switch between different browser windows, tabs, and iframes.

# Working with IFrames
# Switch to iframe by name or ID
driver.switch_to.frame("content-frame")

# Do work inside the frame...
driver.find_element(By.TAG_NAME, "button").click()

# Switch back to main content
driver.switch_to.default_content()

# Working with Windows/Tabs
# Get current window handle
main_window = driver.current_window_handle

# Get all window handles
all_windows = driver.window_handles

# Switch to the new window (usually the last one)
for handle in all_windows:
    if handle != main_window:
        driver.switch_to.window(handle)
        break

# Close new window
driver.close()

# Switch back to main window
driver.switch_to.window(main_window)

Practice Tasks

  • Switch context to the iframe named "checkout".
  • Switch back to the main document content.
  • Switch to the window with handle window_b.

Practice Interview Questions

Test your skills with these real-world Python interview questions. Try to solve them yourself before viewing the solution.

Question 1 of 30

Loading question...

Ready to try it? Open the compiler below: