You’re the Expert!

selenium

Cheatsheets

Setup WebDriver and Open a Website
from selenium import webdriver

# Setup the WebDriver
driver = webdriver.Chrome()

# Open a website
driver.get("https://www.google.com")

# Close the browser
driver.quit()
copy to clipboard
Find Elements by ID and Click
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.google.com")

# Find an element by its ID and click
search_box = driver.find_element_by_id("lst-ib")
search_box.send_keys("Pynfinity Educational Channel")
search_box.submit()

# Close the browser
driver.quit()
copy to clipboard
Find Elements by Class Name
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.google.com")

# Find an element by its class name
result = driver.find_element_by_class_name("g")
print(result.text)

driver.quit()
copy to clipboard
Wait for an Element to Appear
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://www.google.com")

# Wait until the element is visible
WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.ID, "lst-ib"))
)

driver.quit()
copy to clipboard
Interact with a Dropdown
from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome()
driver.get("https://www.yourwebsite.com")

# Locate dropdown
dropdown = driver.find_element_by_id("dropdown_id")
select = Select(dropdown)

# Select option by visible text
select.select_by_visible_text("Option 1")

driver.quit()
copy to clipboard

Working with Multiple Browser Tabs
from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get("https://www.google.com")

# Open a new tab
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])

# Navigate to another website in the new tab
driver.get("https://www.youtube.com")

# Switch back to the first tab
driver.switch_to.window(driver.window_handles[0])

driver.quit()
copy to clipboard
Taking a Screenshot of a Webpage
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.google.com")

# Take a screenshot
driver.save_screenshot("google_screenshot.png")

driver.quit()
copy to clipboard
Handling Alerts in Selenium
from selenium import webdriver
from selenium.webdriver.common.alert import Alert

driver = webdriver.Chrome()
driver.get("https://www.yourwebsite.com")

# Trigger alert
driver.execute_script("alert('This is a test alert!');")

# Switch to alert and accept it
alert = Alert(driver)
alert.accept()

driver.quit()
copy to clipboard
Extract Text from a Web Element
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.example.com")

# Extract text from an element
heading = driver.find_element_by_tag_name("h1")
print("Heading text: ", heading.text)

driver.quit()
copy to clipboard

Handling Dynamic Content with Selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://www.example.com")

# Wait for dynamic content to load
dynamic_content = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "dynamic_content"))
)

print(dynamic_content.text)
driver.quit()
copy to clipboard
Automate Form Submission with Selenium
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.example.com/form")

# Fill out a form
name_input = driver.find_element_by_name("name")
name_input.send_keys("Santosh Kumar")

email_input = driver.find_element_by_name("email")
email_input.send_keys("santosh@example.com")

# Submit the form
submit_button = driver.find_element_by_name("submit")
submit_button.click()

driver.quit()
copy to clipboard