Selenium Cheatsheet
Actions API
Use this Selenium reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
ActionChains Overview
ActionChains queues a series of low-level input actions and dispatches them all with a single .perform() call. Selenium 4 re-implemented the Actions API using the W3C standard input sources.
from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By ac = ActionChains(driver)
Mouse Actions
Click Variants
el = driver.find_element(By.ID, "target") ActionChains(driver).click(el).perform() # left-click ActionChains(driver).double_click(el).perform() # double-click ActionChains(driver).context_click(el).perform() # right-click ActionChains(driver).click_and_hold(el).perform() # press and hold ActionChains(driver).release(el).perform() # release held button
Move to Element
ActionChains(driver).move_to_element(el).perform() # hover # Offset from element center (pixels) ActionChains(driver).move_to_element_with_offset(el, 10, -5).perform() # Offset from current mouse position ActionChains(driver).move_by_offset(100, 50).perform()
Drag and Drop
source = driver.find_element(By.ID, "drag-item") target = driver.find_element(By.ID, "drop-zone") # Simple drag-and-drop ActionChains(driver).drag_and_drop(source, target).perform() # Drag by offset (pixels from source position) ActionChains(driver).drag_and_drop_by_offset(source, 200, 0).perform() # Manual hold-move-release (more reliable in some browsers) ActionChains(driver)\ .click_and_hold(source)\ .pause(0.3)\ .move_to_element(target)\ .pause(0.3)\ .release()\ .perform()
Scroll (Selenium 4.2+)
from selenium.webdriver.common.action_chains import ActionChains # Scroll the viewport by delta pixels ActionChains(driver).scroll_by_amount(0, 500).perform() # scroll down 500px # Scroll an element into view ActionChains(driver).scroll_to_element(el).perform() # Scroll within a scrollable element container = driver.find_element(By.ID, "scrollable-div") ActionChains(driver).scroll_to_element(container).scroll_by_amount(0, 300).perform()
Keyboard Actions
# Key down / key up (hold modifier keys) ActionChains(driver).key_down(Keys.SHIFT).send_keys("hello").key_up(Keys.SHIFT).perform() # Sends "HELLO" # Send keys to focused element ActionChains(driver).send_keys("some text").perform() # Send keys to a specific element ActionChains(driver).send_keys_to_element(el, "typed text").perform() # Keyboard shortcut ActionChains(driver)\ .key_down(Keys.CONTROL)\ .send_keys("a")\ .key_up(Keys.CONTROL)\ .perform() # Ctrl+A (select all)
Common Key Sequences
# Select all and delete ActionChains(driver)\ .click(field)\ .key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL)\ .send_keys(Keys.DELETE)\ .perform() # Shift+click (multi-select) first = driver.find_element(By.CSS_SELECTOR, "li:first-child") last = driver.find_element(By.CSS_SELECTOR, "li:last-child") ActionChains(driver)\ .click(first)\ .key_down(Keys.SHIFT)\ .click(last)\ .key_up(Keys.SHIFT)\ .perform() # Ctrl+click (toggle selection) items = driver.find_elements(By.CSS_SELECTOR, "li.item") ActionChains(driver)\ .click(items[0])\ .key_down(Keys.CONTROL)\ .click(items[2])\ .click(items[4])\ .key_up(Keys.CONTROL)\ .perform()
Chaining Actions
# All steps queued and executed together ActionChains(driver)\ .move_to_element(menu)\ .pause(0.5)\ .click(submenu_trigger)\ .pause(0.3)\ .click(menu_item)\ .perform()
W3C Actions API (Selenium 4 — Low Level)
from selenium.webdriver.common.actions.wheel_input import ScrollOrigin # Scroll from a specific element origin scroll_origin = ScrollOrigin.from_element(el) ActionChains(driver).scroll_from_origin(scroll_origin, 0, 200).perform() # Scroll from element with offset scroll_origin = ScrollOrigin.from_element(el, 0, -50) ActionChains(driver).scroll_from_origin(scroll_origin, 0, 100).perform() # Viewport-origin scroll scroll_origin = ScrollOrigin.from_viewport(10, 10) ActionChains(driver).scroll_from_origin(scroll_origin, 0, 400).perform()
Pause
# Insert a pause (seconds) between chained actions ActionChains(driver)\ .click(el)\ .pause(1.0)\ .send_keys("text")\ .perform()
Reset Actions
ac = ActionChains(driver) ac.click(el) ac.reset_actions() # clear the queued actions without performing
Gotchas
drag_and_dropcan fail on HTML5 drag-and-drop implementations because they rely on synthetic drag events that browsers may ignore. Use a JavaScript-based drag helper or theclick_and_hold→move_to_element→releaseapproach.
key_downmust always be paired withkey_up— an un-released modifier key persists in the browser session and breaks subsequent actions.
Mouse offsets are from the element's center when using
move_to_element_with_offset. Negative values move up/left.
perform()is not cumulative — calling it does not clear the internal queue automatically in older versions. Callreset_actions()or create a newActionChainsinstance for each sequence.