Youโ€™re the Expert!

pynfinity

selenium

Selenium

Your complete recipe for mastering Selenium โ€” course modules & quick-reference guide in one place.

AU Recipe Module 3 sections

BASICS

Setup WebDriver and Open a Website

Explore the concepts and examples below to master this topic.

Code Example
from selenium import webdriver

# Setup the WebDriver
driver = webdriver.Chrome()

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

# Close the browser
driver.quit()
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Find Elements by ID and Click

Explore the concepts and examples below to master this topic.

Code Example
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()
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Find Elements by Class Name

Explore the concepts and examples below to master this topic.

Code Example
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()
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Wait for an Element to Appear

Explore the concepts and examples below to master this topic.

Code Example
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()
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Interact with a Dropdown

Explore the concepts and examples below to master this topic.

Code Example
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()
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

INTERMEDIATE

Working with Multiple Browser Tabs

Explore the concepts and examples below to master this topic.

Code Example
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()
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Taking a Screenshot of a Webpage

Explore the concepts and examples below to master this topic.

Code Example
from selenium import webdriver

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

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

driver.quit()
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Handling Alerts in Selenium

Explore the concepts and examples below to master this topic.

Code Example
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()
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Extract Text from a Web Element

Explore the concepts and examples below to master this topic.

Code Example
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()
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

ADVANCED

Handling Dynamic Content with Selenium

Explore the concepts and examples below to master this topic.

Code Example
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()
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Automate Form Submission with Selenium

Explore the concepts and examples below to master this topic.

Code Example
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()
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.
โšก Quick Reference โ€” Recipe

Use the reference cards below for fast lookup. Perfect for brushing up on syntax while you cook your code. ๐Ÿณ

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()
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()
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()
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()
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()

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()
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()
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()
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()

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()
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()

Recipe Complete! ๐ŸŽ‰

You've explored the full Selenium recipe.
Keep practising โ€” a language a day keeps AI away! ๐Ÿค–

Back to AU Courses


Pynfinity
Install Pynfinity Add to home screen for the best experience