0% found this document useful (0 votes)
7 views

Python Selenium Cheat Sheet

This document is a comprehensive cheat sheet for using Python Selenium, covering installation, browser setup, element interaction, form handling, waiting for elements, and more. It includes code snippets for various tasks such as locating elements, handling alerts, taking screenshots, and executing JavaScript. The guide also addresses advanced user interactions and how to manage browser windows and cookies.

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

Python Selenium Cheat Sheet

This document is a comprehensive cheat sheet for using Python Selenium, covering installation, browser setup, element interaction, form handling, waiting for elements, and more. It includes code snippets for various tasks such as locating elements, handling alerts, taking screenshots, and executing JavaScript. The guide also addresses advanced user interactions and how to manage browser windows and cookies.

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
You are on page 1/ 6

Python Selenium Complete Cheat Sheet

Python Selenium Complete Cheat Sheet

1. Installation & Setup

Install Selenium package:


```python
pip install selenium
```

Download WebDriver (e.g., ChromeDriver) and add to PATH or specify path in code.

Basic import:
```python
from selenium import webdriver
```

2. Launch Browser and Open URL

Create a browser instance and open a webpage:


```python
from selenium import webdriver

driver = webdriver.Chrome() # or webdriver.Firefox()


driver.get("https://www.example.com")
```

3. Locating Elements

Find elements on the page:


```python
element = driver.find_element("id", "element_id")
element = driver.find_element("name", "element_name")
Python Selenium Complete Cheat Sheet

element = driver.find_element("xpath", "//tag[@attr='value']")


element = driver.find_element("css selector", "css.selector")
element = driver.find_element("class name", "class_name")
element = driver.find_element("tag name", "tagname")
element = driver.find_element("link text", "exact link text")
element = driver.find_element("partial link text", "partial text")
```
Find multiple elements:
```python
elements = driver.find_elements("class name", "class_name")
```

4. Interacting with Elements

Common interactions with elements:


```python
element.click() # Click element
element.send_keys("text") # Type text into input
element.clear() # Clear input field
text = element.text # Get visible text
attribute_value = element.get_attribute("attr_name") # Get attribute value
```

5. Handling Forms

Submit forms and handle inputs:


```python
element.submit() # Submit form containing the element
```

Select dropdown option:


```python
Python Selenium Complete Cheat Sheet

from selenium.webdriver.support.ui import Select

select = Select(driver.find_element("id", "dropdown_id"))


select.select_by_visible_text("Option Text")
select.select_by_value("option_value")
select.select_by_index(2)
```

6. Waiting for Elements

Explicit waits (recommended):


```python
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)


element = wait.until(EC.presence_of_element_located((By.ID, "element_id")))
```

Implicit waits (less reliable):


```python
driver.implicitly_wait(10) # Wait up to 10 seconds for elements to appear
```

7. Navigating Pages

Navigate browser history and refresh:


```python
driver.back() # Go back
driver.forward() # Go forward
driver.refresh() # Refresh current page
Python Selenium Complete Cheat Sheet

```

8. Handling Multiple Windows or Tabs

Switch between windows or tabs:


```python
current_window = driver.current_window_handle
all_windows = driver.window_handles

driver.switch_to.window(all_windows[1]) # Switch to second window/tab


```

9. Handling Alerts & Popups

Handle JavaScript alerts, confirms, prompts:


```python
alert = driver.switch_to.alert
alert.accept() # Click OK
alert.dismiss() # Click Cancel
alert.text # Get alert text
alert.send_keys("text") # Type in prompt alert
```

10. Taking Screenshots

Capture screenshot of current page:


```python
driver.save_screenshot("screenshot.png")
```

11. Working with Cookies

Get, add, delete cookies:


Python Selenium Complete Cheat Sheet

```python
cookies = driver.get_cookies()
driver.add_cookie({"name": "foo", "value": "bar"})
driver.delete_cookie("foo")
driver.delete_all_cookies()
```

12. Executing JavaScript

Run JavaScript in browser context:


```python
result = driver.execute_script("return document.title;")
```

13. Working with Frames or iFrames

Switch to iframe and back:


```python
driver.switch_to.frame("frame_name_or_id")
driver.switch_to.default_content() # Back to main page
```

14. Advanced User Interactions

Actions like hover, drag & drop:


```python
from selenium.webdriver import ActionChains

actions = ActionChains(driver)
element = driver.find_element("id", "element_id")
actions.move_to_element(element).perform() # Hover
actions.click_and_hold(source).move_to_element(target).release().perform() # Drag and drop
```
Python Selenium Complete Cheat Sheet

15. Closing Browser

Close browser window or quit driver:


```python
driver.close() # Close current window
driver.quit() # Close all windows and end session
```

You might also like