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

Selenium Python Cheat Sheet

This document is a cheat sheet for using Selenium with Python, covering installation, importing libraries, driver initialization, setting options, locators, actions, waits, taking screenshots, and teardown. It provides code snippets for each section to facilitate quick reference. The cheat sheet is designed for users to efficiently automate web browser interactions using Selenium.

Uploaded by

hello.softworica
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)
17 views

Selenium Python Cheat Sheet

This document is a cheat sheet for using Selenium with Python, covering installation, importing libraries, driver initialization, setting options, locators, actions, waits, taking screenshots, and teardown. It provides code snippets for each section to facilitate quick reference. The cheat sheet is designed for users to efficiently automate web browser interactions using Selenium.

Uploaded by

hello.softworica
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/ 1

Selenium Python Cheat Sheet

1. Installation
- Install Selenium via pip:
pip install selenium # ■cite■turn0view0■

2. Importing:
from selenium import webdriver # ■cite■turn0view0■

3. Driver Initialization:
driver = webdriver.Chrome() # ■cite■turn0view0■
driver = webdriver.Firefox() # ■cite■turn0view0■

4. Setting Options:
from selenium.webdriver.chrome.options import Options # ■cite■turn0view0■
chrome_options = Options()
chrome_options.add_argument('--disable-extensions')
driver = webdriver.Chrome(options=chrome_options)

5. Locators:
- By ID: driver.find_element_by_id('id') # ■cite■turn0view0■
- By Name: driver.find_element_by_name('name') # ■cite■turn0view0■
- By Class: driver.find_element_by_class_name('class') # ■cite■turn0view0■
- By XPath: driver.find_element_by_xpath('//tag[@attr]') # ■cite■turn0view0■
- By CSS: driver.find_element_by_css_selector('css') # ■cite■turn0view0■

6. Actions:
element.click() # ■cite■turn0view0■
element.send_keys('text') # ■cite■turn0view0■

7. Waits:
from selenium.webdriver.common.by import By # ■cite■turn0view0■
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'id'))) # ■cite■turn0view0■

8. Screenshots:
driver.save_screenshot('screen.png') # ■cite■turn0view0■

9. Teardown:
driver.quit() # ■cite■turn0view0■

You might also like