0% found this document useful (0 votes)
4 views3 pages

Selenium Python CheatSheet

This document is a comprehensive cheat sheet for using Selenium with Python, covering setup, browser control, element finding, actions on elements, and handling dropdowns, alerts, iframes, and multiple windows. It includes code snippets for common tasks such as clicking, typing, and executing JavaScript, as well as managing waits and taking screenshots. Additionally, it provides guidance on file uploads and mouse/keyboard actions.
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)
4 views3 pages

Selenium Python CheatSheet

This document is a comprehensive cheat sheet for using Selenium with Python, covering setup, browser control, element finding, actions on elements, and handling dropdowns, alerts, iframes, and multiple windows. It includes code snippets for common tasks such as clicking, typing, and executing JavaScript, as well as managing waits and taking screenshots. Additionally, it provides guidance on file uploads and mouse/keyboard actions.
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

Selenium with Python - Easy & Detailed Cheat

Sheet

1. Setup & Browser Control

- Import Selenium and start browser:


from selenium import webdriver
driver = [Link]() # Launch Chrome browser
[Link]("[Link] # Open website
driver.maximize_window() # Maximize window
driver.minimize_window() # Minimize window
[Link]() # Refresh page
[Link]() # Go back
[Link]() # Go forward
[Link]() # Close whole browser
[Link]() # Close current tab

2. Finding Elements

- Find a single element:


element = driver.find_element("id", "username")

- Find multiple elements:


elements = driver.find_elements("class name", "product")

- Locator types you can use:


find_element("id", "value")
find_element("name", "value")
find_element("class name", "value")
find_element("tag name", "value")
find_element("link text", "Click Here")
find_element("css selector", "[Link] > a")
find_element("xpath", "//input[@type='text']")

3. Actions on Elements

- Clicking and typing:


[Link]() # Click on element
element.send_keys("hello") # Type text
[Link]() # Clear input box

- Getting information:
[Link] # Get inner text
element.get_attribute("href") # Get attribute value

4. Keyboard Actions
- Use Keys class for keyboard buttons:
from [Link] import Keys

element.send_keys([Link]) # Press Enter


element.send_keys([Link]) # Press Tab
element.send_keys([Link]) # Press Escape

5. Mouse Actions

- Use ActionChains for advanced mouse actions:


from [Link] import ActionChains

actions = ActionChains(driver)
actions.move_to_element(element).perform() # Hover mouse
actions.double_click(element).perform() # Double click
actions.context_click(element).perform() # Right click
actions.drag_and_drop(source, target).perform() # Drag & drop

6. Dropdowns

- Work with select menus:


from [Link] import Select

dropdown = Select(driver.find_element("id", "country"))


dropdown.select_by_visible_text("Pakistan")
dropdown.select_by_value("PK")
dropdown.select_by_index(2)

7. Alerts & Popups

- Handling alert popups:


alert = driver.switch_to.alert
[Link] # Get alert message
[Link]() # Click OK
[Link]() # Click Cancel
alert.send_keys("text") # Type into alert (prompt)

8. Iframes

- Switch to an iframe:
driver.switch_to.frame("frameName")

- Go back to main page:


driver.switch_to.default_content()

9. Windows & Tabs


- Handling multiple tabs/windows:
driver.current_window_handle # Get current tab
driver.window_handles # Get all open tabs
driver.switch_to.window(driver.window_handles[1]) # Switch tab

10. Waits

- Implicit wait (set once for all elements):


driver.implicitly_wait(10)

- Explicit wait (wait for specific element):


from [Link] import By
from [Link] import WebDriverWait
from [Link] import expected_conditions as EC

wait = WebDriverWait(driver, 10)


element = [Link](EC.presence_of_element_located(([Link], "login")))

11. JavaScript Execution

- Run custom JavaScript:


driver.execute_script("[Link](0, [Link])") # Scroll down
driver.execute_script("arguments[0].click();", element) # Click with JS

12. Screenshots

- Save screenshot of page:


driver.save_screenshot("[Link]")

13. File Upload

- Upload a file into input field:


upload = driver.find_element("id", "file")
upload.send_keys("C:/path/to/[Link]")

You might also like