Master browser automation with Selenium. Practice syntax, locators, and interactions in a safe, static environment.
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()
webdriver.Chrome() instance named
driver.
driver.get().
page_title.
driver.quit().
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")
//div[@id='content'].
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()
login_button.
email_field.
username_input.
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()
wait with a 10-second
timeout.
EC.presence_of_element_located to wait for ID "header".
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
Select object using element with ID "dates".
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()
ActionChains instance named actions.
my_btn.
menu_item and perform the action.
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)
window_b.
Test your skills with these real-world Python interview questions. Try to solve them yourself before viewing the solution.
Ready to try it? Open the compiler below: