Python Selenium Cheat Sheet (1)
Python Selenium Cheat Sheet (1)
Download WebDriver:
- ChromeDriver: https://sites.google.com/chromium.org/driver/
- GeckoDriver: https://github.com/mozilla/geckodriver/releases
Add the driver executable to your system PATH or specify its location in code.
Example:
# Open a webpage
driver.get("https://www.google.com")
Example:
# Find element by ID
Python Selenium Complete Cheat Sheet
Beginner to Advanced | With Code Examples & Descriptions
Example:
Example:
Implicit wait:
Example:
driver.back() # Go back
driver.forward() # Go forward
driver.refresh() # Refresh current page
Example:
Python Selenium Complete Cheat Sheet
Beginner to Advanced | With Code Examples & Descriptions
Example:
alert = driver.switch_to.alert
# Accept alert
alert.accept()
# Dismiss alert
alert.dismiss()
Example:
driver.save_screenshot("screenshot.png")
Example:
# Add cookie
driver.add_cookie({"name": "foo", "value": "bar"})
# Delete cookie
driver.delete_cookie("foo")
Example:
Example:
Example:
actions = ActionChains(driver)
element = driver.find_element("id", "element_id")
Example:
Example:
try:
element = driver.find_element("id", "does_not_exist")
except NoSuchElementException:
print("Element not found")
except TimeoutException:
print("Timed out waiting for element")
Python Selenium Complete Cheat Sheet
Beginner to Advanced | With Code Examples & Descriptions