Let’s be honest – when you’re first learning Selenium, the documentation can feel overwhelming. I spent way too much time digging through endless pages just to find the one command I needed to click a button or enter text in a field.
That’s exactly why I created this cheat sheet. After months of fumbling through Selenium tutorials and real testing scenarios, I’ve compiled every command I actually use in my day-to-day automation work. No fluff, no complicated examples – just the practical stuff you need to get things done.
Bonus: I’ve created a live Notion page with all these commands that you can bookmark and access from anywhere while you’re coding!
Why I Made This Cheat Sheet
Picture this: You’re in the middle of writing a test script, you know exactly what you want to do (click that login button!), but you can’t remember the exact syntax. Sound familiar?
I got tired of constantly Googling “how to click element selenium python” for the hundredth time. So I created this reference guide with all the commands I use regularly, organized the way my brain actually thinks about them.
Career Changeup Tip: Having a personal cheat sheet isn’t cheating – it’s smart! Even experienced developers keep reference materials handy. It’s better to have quick access to correct syntax than to guess and spend time debugging.
The Essential Selenium Setup
Every Selenium script starts the same way. Here’s the basic setup I use:
from selenium import webdriver
from selenium.webdriver.common.by import By
# Open browser
driver = webdriver.Chrome()
driver.implicitly_wait(10) # Wait up to 10 seconds for elements
driver.get("https://example.com") # Navigate to your page
My Learning Moment: I used to forget the implicitly_wait() line and then wonder why my tests were failing randomly. This tells Selenium to wait a reasonable amount of time for elements to appear before giving up.
Browser Navigation: Getting Around
These are the commands for basic browser actions:
| Action | Code |
|---|---|
| Open URL | driver.get("http://...") |
| Go back | driver.back() |
| Go forward | driver.forward() |
| Refresh page | driver.refresh() |
| Close browser | driver.quit() |
Pro Tip: Always use driver.quit() at the end of your scripts. I learned this the hard way when I had 20 browser windows open after running tests!
Finding Elements: The Foundation of Everything
This is where most beginners get stuck. Here are all the ways to find elements on a page:
By ID (Most Reliable)
element = driver.find_element(By.ID, "username")
Use this when the element has a unique ID – it’s the most reliable method.
By Name
element = driver.find_element(By.NAME, "password")
Great for form fields that have name attributes.
By Class Name
element = driver.find_element(By.CLASS_NAME, "btn-primary")
Useful for styled elements, but be careful – multiple elements might have the same class.
By Tag Name
element = driver.find_element(By.TAG_NAME, "input")
Finds the first element with that HTML tag.
By Link Text
element = driver.find_element(By.LINK_TEXT, "Login")
Perfect for clicking links when you know the exact text.
By Partial Link Text
element = driver.find_element(By.PARTIAL_LINK_TEXT, "Sign")
When you only know part of the link text.
By CSS Selector
element = driver.find_element(By.CSS_SELECTOR, "input#username")
More flexible than basic methods – great when you know CSS.
By XPath
element = driver.find_element(By.XPATH, "//input[@id='username']")
Most powerful but can be complex. I use this as a last resort.
Tech Toolkit of the Week: Browser Developer Tools
The secret to finding elements is using your browser’s developer tools. Right-click on any element and select “Inspect” – you’ll see all the attributes you can use to locate it. This has saved me countless hours of guessing!
Interacting with Elements: Making Things Happen
Once you find an element, here’s what you can do with it:
| Action | Code |
|---|---|
| Type text | element.send_keys("hello world") |
| Click | element.click() |
| Clear field | element.clear() |
| Get text content | text = element.text |
| Get attribute value | value = element.get_attribute("value") |
| Check if visible | is_visible = element.is_displayed() |
| Check if enabled | is_enabled = element.is_enabled() |
Side Hustle Strategy: Practice these basic interactions on any website. Go to a login page and try automating the process – it’s the best way to get comfortable with the syntax.
Waiting Strategies: Avoiding the “Element Not Found” Nightmare
Nothing is more frustrating than tests that fail because they ran too fast. Here are the waiting strategies that actually work:
Implicit Wait (Set Once, Use Everywhere)
driver.implicitly_wait(10)
This tells Selenium to wait up to 10 seconds for any element before giving up.
Explicit Wait (For Specific Situations)
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Wait for a specific element to appear
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "submit-button"))
)
My Learning Moment: I used to use time.sleep(5) everywhere, which made my tests slow and unreliable. Learning proper waits was a game-changer for test stability.
Useful Extras: The Commands That Save the Day
These aren’t everyday commands, but when you need them, you REALLY need them:
Taking Screenshots
driver.save_screenshot("test_failure.png")
Essential for debugging failed tests!
Handling Alerts
alert = driver.switch_to.alert
alert.accept() # Click OK
# or
alert.dismiss() # Click Cancel
Working with Frames
# Switch to a frame
driver.switch_to.frame("frame_name")
# Switch back to main page
driver.switch_to.default_content()
Switching Between Tabs/Windows
# Switch to second tab
driver.switch_to.window(driver.window_handles[1])
Real-World Example: A Complete Login Test
Here’s how all these commands work together in a real test:
from selenium import webdriver
from selenium.webdriver.common.by import By
# Setup
driver = webdriver.Chrome()
driver.implicitly_wait(10)
try:
# Navigate to login page
driver.get("https://example.com/login")
# Find and fill username
username_field = driver.find_element(By.ID, "username")
username_field.clear()
username_field.send_keys("testuser@example.com")
# Find and fill password
password_field = driver.find_element(By.ID, "password")
password_field.clear()
password_field.send_keys("password123")
# Click login button
login_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit']")
login_button.click()
# Verify we're logged in
welcome_message = driver.find_element(By.CLASS_NAME, "welcome")
assert "Welcome" in welcome_message.text
print("Login test passed!")
except Exception as e:
print(f"Test failed: {e}")
driver.save_screenshot("login_test_failure.png")
finally:
driver.quit()
Pro Tips I Wish Someone Had Told Me
- Always use
try/finally– This ensures your browser closes even if the test fails - Avoid hard sleeps – Use waits instead of
time.sleep() - Start with simple locators – ID and name are usually the most reliable
- Take screenshots on failure – Future you will thank present you
- Keep your cheat sheet handy – No shame in referencing it!
Download Your Free Cheat Sheet
I’ve created a comprehensive Notion page with all these commands organized and searchable. You can bookmark it, access it from anywhere, and I’ll keep it updated as new Selenium features come out.
Access the Live Selenium Python Cheat Sheet →
Let’s Practice Together!
What Selenium commands do you find yourself looking up most often? Are there any automation scenarios you’re struggling with? Drop your questions in the comments and let’s figure them out together!
