1. Install Selenium:
pip install selenium
pip install webdriver_manager
A rapid reference guide for developers and QA automation engineers using Selenium with Python to automate web browser interactions. Covers setup, element location, interactions, waits, navigation, screenshots, and common error handling.
1. Install Selenium:
|
2. Basic Imports:
|
3. Launching Browsers (Chrome Example):
|
4. Headless Mode (Chrome):
|
Pro Tip: Use |
|
|
By.ID |
|
By.NAME |
|
By.CLASS_NAME |
|
By.TAG_NAME |
|
By.LINK_TEXT / By.PARTIAL_LINK_TEXT |
|
By.XPATH |
|
By.CSS_SELECTOR |
|
Pro Tip: Prioritize |
Clicking an element |
|
Typing into a text field |
|
Clearing text |
|
Sending special keys |
|
Dropdowns (Select class) |
|
Checkboxes & Radio Buttons |
|
Getting element text / attribute |
|
Common Pitfall: |
|
Note: This is a fixed delay and should be avoided in most cases as it makes tests slow and brittle. |
Implicit Wait |
Explanation: Applied globally for the WebDriver instance. Selenium will poll the DOM for a certain amount of time when trying to find an element. |
Explicit Wait ( |
Explanation: Waits for a specific condition to be met before proceeding. More flexible and robust than implicit waits. |
Common |
|
Custom Wait Conditions |
|
Pro Tip: Use explicit waits for specific conditions, especially before interacting with dynamic elements. Use implicit waits as a fallback for general element presence, but be cautious as they can sometimes conflict with explicit waits causing unexpected delays. |
Navigate to a URL |
|
Browser history |
|
Window size & position |
|
Close vs Quit |
|
Switching Windows/Tabs |
|
Switching Frames |
|
Common Pitfall: Forgetting to call |
Capture full page screenshot |
|
Capture element screenshot |
|
Basic Python Logging |
|
Pro Tip: Include timestamps in your screenshot filenames (e.g., |
|
Cause: Element not found on the page or not present in the DOM when searched. |
|
Cause: An explicit wait condition was not met within the specified timeout duration. |
|
Cause: Element is present but not in a state to be interacted with (e.g., hidden, disabled, overlaid). |
|
Cause: The element reference you hold is no longer attached to the DOM (e.g., page refresh, element reloaded). |
Using |
|
Debugging Tips |
|
Pro Tip: Most common Selenium errors stem from timing issues. Master explicit waits and understand the state of your application’s DOM to write robust and reliable automation scripts. |