0% found this document useful (0 votes)
239 views5 pages

Python Selenium Cheat Sheet Guide

This document is a comprehensive cheat sheet for using Python Selenium, covering installation, basic setup, and various functionalities such as finding elements, handling input fields, interacting with buttons, and managing alerts. It also includes advanced topics like working with multiple windows, executing JavaScript, and using Selenium Grid for remote testing. The guide provides code examples and descriptions for each feature, making it suitable for both beginners and advanced users.

Uploaded by

arw1362
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
239 views5 pages

Python Selenium Cheat Sheet Guide

This document is a comprehensive cheat sheet for using Python Selenium, covering installation, basic setup, and various functionalities such as finding elements, handling input fields, interacting with buttons, and managing alerts. It also includes advanced topics like working with multiple windows, executing JavaScript, and using Selenium Grid for remote testing. The guide provides code examples and descriptions for each feature, making it suitable for both beginners and advanced users.

Uploaded by

arw1362
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Selenium Complete Cheat Sheet

Beginner to Advanced | With Code Examples & Descriptions

1. 1. Getting Started with Selenium


Selenium is used for automating web applications for testing purposes. It
supports multiple languages including Python.
Installation:

pip install selenium


# Also install the browser driver like ChromeDriver or GeckoDriver (Firefox)
# Download from: [Link]

2. 2. Basic WebDriver Setup


Launch the browser and open a website:

from selenium import webdriver


driver = [Link]()
[Link]("[Link]
[Link]()

3. 3. Finding Elements
Different ways to locate web elements:

driver.find_element("id", "element_id")
driver.find_element("name", "element_name")
driver.find_element("xpath", "//tag[@attribute='value']")
driver.find_element("css selector", "[Link]")

4. 4. Working with Input Fields


Type into input boxes, clear them, and submit forms:

input_box = driver.find_element("name", "q")


input_box.send_keys("Selenium Python")
input_box.clear()
input_box.submit()

5. 5. Clicking and Interacting with Buttons


Clicking buttons or links:
Python Selenium Complete Cheat Sheet
Beginner to Advanced | With Code Examples & Descriptions

button = driver.find_element("id", "submit")


[Link]()

6. 6. Dropdowns and Select Boxes


Using Select class to handle dropdown menus:

from [Link] import Select


select = Select(driver.find_element("id", "dropdown"))
select.select_by_visible_text("Option 1")
select.select_by_index(1)
select.select_by_value("value1")

7. 7. Waiting for Elements


Implicit and Explicit waits to handle dynamic loading:

# Implicit Wait
driver.implicitly_wait(10)

# Explicit Wait
from [Link] import By
from [Link] import WebDriverWait
from [Link] import expected_conditions as EC

wait = WebDriverWait(driver, 10)


element = [Link](EC.visibility_of_element_located(([Link], 'some_id')))

8. 8. Handling Alerts
Handle JavaScript alerts, confirms, and prompts:

alert = driver.switch_to.alert
[Link]() # OK
[Link]() # Cancel
alert.send_keys("Text")

9. 9. Screenshots and Page Source


Capture screenshots or get the HTML source:
Python Selenium Complete Cheat Sheet
Beginner to Advanced | With Code Examples & Descriptions

driver.save_screenshot("[Link]")
html = driver.page_source

10. 10. Navigating Between Pages


Move back, forward, and refresh:

[Link]()
[Link]()
[Link]()

11. 11. Working with Multiple Windows


Switch between tabs or windows:

main_window = driver.current_window_handle
handles = driver.window_handles
driver.switch_to.window(handles[1]) # Switch to new tab

12. 12. Frames and iFrames


Switch to a frame and back:

driver.switch_to.frame("frameName")
driver.switch_to.default_content()

13. 13. Executing JavaScript


Run JavaScript commands:

title = driver.execute_script("return [Link]")


print(title)

14. 14. Mouse Actions


Use ActionChains for hover, drag-drop, etc.:

from [Link] import ActionChains


actions = ActionChains(driver)
element = driver.find_element("id", "hover")
actions.move_to_element(element).perform()
Python Selenium Complete Cheat Sheet
Beginner to Advanced | With Code Examples & Descriptions

15. 15. File Uploads


Send a file path to an input field:

upload = driver.find_element("id", "upload")


upload.send_keys("C:/path/to/[Link]")

16. 16. Download Files


Use browser options to specify download folder:

from selenium import webdriver


options = [Link]()
prefs = {"download.default_directory": "C:\\Downloads"}
options.add_experimental_option("prefs", prefs)
driver = [Link](options=options)

17. 17. Handling Cookies


Read, add, or delete cookies:

driver.get_cookies()
driver.add_cookie({"name": "test", "value": "value"})
driver.delete_all_cookies()

18. 18. Headless Mode


Run browser without GUI:

from selenium import webdriver


options = [Link]()
options.add_argument("--headless")
driver = [Link](options=options)

19. 19. Selenium Grid and Remote WebDriver


Run tests remotely using a hub and node:

from selenium import webdriver


driver = [Link](
command_executor='[Link]
Python Selenium Complete Cheat Sheet
Beginner to Advanced | With Code Examples & Descriptions

desired_capabilities={"browserName": "chrome"})

20. 20. Exception Handling


Catch common Selenium exceptions:

from [Link] import NoSuchElementException


try:
driver.find_element("id", "missing")
except NoSuchElementException:
print("Element not found")

You might also like