20 Selenium concepts with Before-and-After Examples
1. Setting Up WebDriver (ChromeDriver, GeckoDriver) 🚗
Boilerplate Code:
from selenium import webdriver
Use Case: Set up WebDriver for different browsers (Chrome, Firefox, etc.). 🚗
Goal: Launch a web browser that you can automate. 🎯
Sample Code:
# Chrome WebDriver
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
# Firefox (GeckoDriver)
driver = webdriver.Firefox(executable_path='/path/to/geckodriver')
Before Example:
You want to automate browser actions but don’t have the WebDriver set up. 🤔
No browser automation available.
After Example:
With WebDriver, you can now open a browser and control it with Selenium! 🚗
Chrome browser launched.
Challenge: 🌟 Try setting up WebDriver for different browsers (Edge, Safari, etc.).
2. Opening a Web Page (get method) 🌐
Boilerplate Code:
driver.get('url')
Use Case: Use get to navigate to a web page. 🌐
Goal: Open a specific URL in the automated browser. 🎯
Sample Code:
# Open a web page
driver.get('https://www.example.com')
Before Example:
You have a browser open but it’s not navigating to the website you want. 🤔
Browser opened but no webpage loaded.
After Example:
With get, the browser navigates to the specified URL! 🌐
Example.com is loaded in the browser.
Challenge: 🌟 Try opening multiple pages one after another.
3. Finding Elements by ID (find_element_by_id) 🔍
Boilerplate Code:
element = driver.find_element_by_id('element_id')
Use Case: Use find_element_by_id to locate an element by its ID. 🔍
Goal: Interact with or extract information from a specific element. 🎯
Sample Code:
# Find an element by ID
element = driver.find_element_by_id('submit-button')
Before Example:
You want to interact with an element but don’t know how to locate it by ID. 🤔
Element not found.
After Example:
With find_element_by_id, you can locate and interact with the element! 🔍
Element with ID 'submit-button' found.
Challenge: 🌟 Try finding and interacting with different elements like buttons, input fields, etc.
4. Finding Elements by Class Name (find_element_by_class_name) 🎯
Boilerplate Code:
element = driver.find_element_by_class_name('class_name')
Use Case: Use find_element_by_class_name to locate an element by its class. 🎯
Goal: Select elements using their class name. 🎯
Sample Code:
# Find an element by class name
element = driver.find_element_by_class_name('btn-primary')
Before Example:
You can’t find an element using ID but know its class name. 🤔
Element not located by ID.
After Example:
With find_element_by_class_name, you can locate elements by class! 🎯
Element with class 'btn-primary' found.
Challenge: 🌟 Try locating multiple elements with the same class and interacting with them.
5. Interacting with Elements (click method) 🖱️
Boilerplate Code:
element.click()
Use Case: Use click to interact with buttons, links, and other clickable elements. 🖱️
Goal: Perform a click action on an element. 🎯
Sample Code:
# Click a button
submit_button = driver.find_element_by_id('submit')
submit_button.click()
Before Example:
You’ve located a button but can’t trigger a click event on it. 🤔
Button located, but not clicked.
After Example:
With click, you can perform a click event just like a human user! 🖱️
Button clicked.
Challenge: 🌟 Try interacting with other clickable elements like checkboxes, radio buttons, etc.
6. Entering Text in Input Fields (send_keys) ⌨️
Boilerplate Code:
element.send_keys('text')
Use Case: Use send_keys to enter text into an input field. ⌨️
Goal: Simulate typing text into form fields. 🎯
Sample Code:
# Enter text in a text box
text_box = driver.find_element_by_id('username')
text_box.send_keys('myusername')
Before Example:
You’ve located a form field but don’t know how to enter text into it. 🤔
Form field found, but no text entered.
After Example:
With send_keys, text is automatically typed into the input field! ⌨️
Text 'myusername' entered into the form.
Challenge: 🌟 Try entering text into multiple form fields, like password, email, etc.
7. Submitting Forms (submit method) 📋
Boilerplate Code:
element.submit()
Use Case: Use submit to submit a form after entering values. 📋
Goal: Automatically submit a form as if a user pressed "Submit". 🎯
Sample Code:
# Submit the form
form = driver.find_element_by_id('login-form')
form.submit()
Before Example:
You’ve entered data into a form but haven’t submitted it. 🤔
Form data entered, but not submitted.
After Example:
With submit, the form is automatically submitted! 📋
Form submitted.
Challenge: 🌟 Try filling out a login form and submitting it.
8. Waiting for Elements (implicitly_wait) ⏳
Boilerplate Code:
driver.implicitly_wait(10)
Use Case: Use implicitly_wait to tell the WebDriver to wait until an element is available. ⏳
Goal: Handle dynamically loaded content by waiting for elements to load. 🎯
Sample Code:
# Wait for elements to load
driver.implicitly_wait(10) # Wait up to 10 seconds for elements to appear
Before Example:
Your script crashes because elements haven’t fully loaded yet. 🤔
NoSuchElementException due to missing element.
After Example:
With implicitly_wait, your script waits for elements to load before interacting! ⏳
No errors! Script waits for elements to appear.
Challenge: 🌟 Try experimenting with different wait times and see how it affects dynamic pages.
9. Closing the Browser (close and quit methods) 🛑
Boilerplate Code:
driver.close()
driver.quit()
Use Case: Use close or quit to stop the browser session. 🛑
Goal: Gracefully close the browser after the task is completed. 🎯
Sample Code:
# Close the current window
driver.close()
# Quit the entire session (close all windows)
driver.quit()
Before Example:
The browser remains open after the automation completes. 🤔
Browser is still running.
After Example:
With close and quit, the browser closes properly! 🛑
Browser session closed.
Challenge: 🌟 Try opening multiple windows or tabs and close them one by one.
10. Handling Alerts (switch_to.alert) 🚨
Boilerplate Code:
driver.switch_to.alert
Use Case: Use switch_to.alert to interact with JavaScript alerts, prompts, and confirmations. 🚨
Goal: Handle pop-ups like alerts or confirmation dialogs. 🎯
Sample Code:
# Handle an alert pop-up
alert = driver.switch_to.alert
alert.accept() # Click OK on the alert
Before Example:
Your script is stuck because a JavaScript alert is blocking further actions. 🤔
Script is paused due to an alert.
After Example:
With switch_to.alert, you can handle the pop-up automatically! 🚨
Alert accepted, script continues.
Challenge: 🌟 Try handling alerts with dismiss() (Cancel) and input
fields in prompt() dialogs.
11. Taking Screenshots (save_screenshot) 📸
Boilerplate Code:
driver.save_screenshot('screenshot.png')
Use Case: Capture screenshots of the current browser window. 📸
Goal: Save a screenshot of the webpage for debugging or record-keeping purposes. 🎯
Sample Code:
# Save a screenshot
driver.save_screenshot('homepage_screenshot.png')
Before Example:
You want to take a screenshot but don’t know how to capture it. 🤔
No visual evidence of browser automation.
After Example:
With save_screenshot, you can easily capture a screenshot of the webpage! 📸
Screenshot saved as 'homepage_screenshot.png'.
Challenge: 🌟 Try taking screenshots at different points in your script to capture changes over time.
12. Handling Frames (switch_to.frame) 🖼️
Boilerplate Code:
driver.switch_to.frame('frame_name_or_index')
Use Case: Interact with elements inside iframe elements by switching to the frame. 🖼️
Goal: Switch to a specific frame to interact with elements inside it. 🎯
Sample Code:
# Switch to an iframe
driver.switch_to.frame('iframe_name')
Before Example:
You want to interact with elements inside an iframe but can’t access them directly. 🤔
Element is inside an iframe, interaction blocked.
After Example:
With switch_to.frame, you can access and interact with elements inside iframes! 🖼️
Iframe switched, elements now accessible.
Challenge: 🌟 Try switching between multiple frames or back to the default content (driver.switch_to.default_content()).
13. Executing JavaScript (execute_script) 💻
Boilerplate Code:
driver.execute_script('javascript_code')
Use Case: Run JavaScript code directly in the browser. 💻
Goal: Execute custom JavaScript to interact with the webpage. 🎯
Sample Code:
# Execute JavaScript to scroll to the bottom of the page
driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
Before Example:
You need to perform actions like scrolling or interacting with JavaScript but don’t know how. 🤔
No direct way to perform custom JavaScript actions.
After Example:
With execute_script, you can run JavaScript to perform custom tasks! 💻
Custom JavaScript executed, page scrolled to the bottom.
Challenge: 🌟 Try using JavaScript to modify elements, change styles, or trigger events on the page.
14. Handling Multiple Windows (window_handles) 🪟
Boilerplate Code:
driver.switch_to.window('window_handle')
Use Case: Switch between multiple browser windows or tabs. 🪟
Goal: Handle multiple browser windows and switch between them as needed. 🎯
Sample Code:
# Get all window handles
windows = driver.window_handles
# Switch to the second window
driver.switch_to.window(windows[1])
Before Example:
You open a new tab or window but can’t switch to it. 🤔
Browser opens new window, but you stay in the first one.
After Example:
With window_handles, you can switch to any open window or tab! 🪟
Switched to the second window.
Challenge: 🌟 Try opening multiple windows or tabs and switching between them dynamically.
15. Handling Drop-down Menus (Select class) 🔽
Boilerplate Code:
from selenium.webdriver.support.ui import Select
Use Case: Interact with drop-down menus and select options by value or index. 🔽
Goal: Select options from a drop-down menu programmatically. 🎯
Sample Code:
# Find the drop-down menu element
select_element = driver.find_element_by_id('dropdown')
# Create a Select object
select = Select(select_element)
# Select an option by visible text
select.select_by_visible_text('Option 1')
Before Example:
You can’t interact with a drop-down menu using regular methods. 🤔
Drop-down menu not accessible.
After Example:
With Select, you can easily interact with drop-down menus! 🔽
Drop-down menu option selected.
Challenge: 🌟 Try selecting options by value (select_by_value) or by index (select_by_index).
16. Waiting for Specific Conditions (WebDriverWait) ⏲️
Boilerplate Code:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Use Case: Use WebDriverWait to wait for specific conditions, like element visibility. ⏲️
Goal: Dynamically wait until elements meet specific conditions (e.g., clickable, visible). 🎯
Sample Code:
# Wait for an element to be clickable
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, 'my-button'))
)
element.click()
Before Example:
You want to interact with an element, but it hasn’t fully loaded. 🤔
Script fails because element isn't ready.
After Example:
With WebDriverWait, you wait until the element is ready to interact! ⏲️
Element becomes clickable, and interaction is successful.
Challenge: 🌟 Try waiting for other conditions, like visibility_of_element_located or presence_of_element_located.
17. Handling File Uploads (send_keys) 📤
Boilerplate Code:
element.send_keys('/path/to/file')
Use Case: Simulate a file upload by sending the file path to an input element. 📤
Goal: Automate file uploads by interacting with input elements. 🎯
Sample Code:
# Find the file upload input and upload a file
file_input = driver.find_element_by_id('file-upload')
file_input.send_keys('/path/to/file.jpg')
Before Example:
You need to upload a file, but there’s no simple way to interact with the file dialog. 🤔
Manual file upload required.
After Example:
With send_keys, you can automate file uploads! 📤
File uploaded programmatically.
Challenge: 🌟 Try automating the upload of different file types (e.g., images, documents, etc.).
18. Downloading Files with Requests and Selenium 📥
Boilerplate Code:
import requests
Use Case: Use requests with Selenium to automate file downloads from links. 📥
Goal: Download files from a webpage using Selenium to navigate and Requests to fetch the file. 🎯
Sample Code:
# Extract the download link using Selenium
download_link = driver.find_element_by_id('download-link').get_attribute('href')
# Download the file using Requests
response = requests.get(download_link)
with open('downloaded_file.zip', 'wb') as file:
file.write(response.content)
Before Example:
You find a download link but don’t know how to automate the file download. 🤔
File download requires manual clicking.
After Example:
With Selenium and Requests, you can fully automate the download! 📥
File downloaded automatically.
Challenge: 🌟 Try downloading different file types, like PDFs, images, or ZIP files.
19. Maximizing Browser Window (maximize_window) 📏
Boilerplate Code:
driver.maximize_window()
Use Case: Maximize the browser window for optimal display. 📏
Goal: Ensure the browser is fully maximized for testing or scraping. 🎯
Sample Code:
# Maximize the browser window
driver.maximize_window()
Before Example:
The browser window is small, which may affect how elements are displayed. 🤔
Browser window is minimized or not fully visible.
After Example:
With maximize_window, the browser is now in full-screen mode! 📏
Browser window maximized.
Challenge: 🌟 Try resizing the window to specific dimensions using driver.set_window_size(width, height).
20. Closing Pop-ups (Switching Windows and Alerts) 📡
Boilerplate Code:
driver.switch_to.window('window_handle')
driver.switch_to.alert.accept()
Use Case: Close pop-up windows or alerts using Selenium. 📡
Goal
: Automatically handle and close pop-ups that interrupt your workflow. 🎯
Sample Code:
# Switch to the pop-up window and close it
pop_up_window = driver.window_handles[-1]
driver.switch_to.window(pop_up_window)
driver.close()
# Handle and accept an alert pop-up
driver.switch_to.alert.accept()
Before Example:
Your script gets stuck on pop-ups or alert dialogs. 🤔
Script interrupted by pop-ups.
After Example:
With window switching and alert handling, pop-ups are automatically closed! 📡
Pop-ups handled and closed, script continues smoothly.
Challenge: 🌟 Try handling multiple pop-ups in sequence, and experiment with dismissing alerts (.dismiss()).