Selenium + Python · Lesson 22 of 43

0/43 done

Module 22: Mouse & Keyboard Actions

22. ActionChains: Mouse Hover, Context Clicks & Drag and Drop

16 minlesson

Your task

Implement hover_and_click(driver, hover_elem, click_elem):

  1. ActionChains(driver).move_to_element(hover_elem).click(click_elem).perform().

Write it in the editor, then press Check answer (Ctrl + Enter).

Chaining Actions in Python

python
from selenium.webdriver.common.action_chains import ActionChains

actions = ActionChains(driver)

menu = driver.find_element(By.ID, "nav-menu")
actions.move_to_element(menu).perform()

# Drag and drop:
source = driver.find_element(By.ID, "draggable")
target = driver.find_element(By.ID, "droppable")
actions.drag_and_drop(source, target).perform()

Key Takeaways & Walkthrough Summary

Complex hardware gestures like hovering over navigation flyouts, right-clicking, and drag-and-drop are performed using ActionChains in Python. In this lesson, you will chain actions and execute them using perform(). Let's master ActionChains!

Part of the Selenium + Python course (Selenium with Python). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why did my ActionChains command have no effect on the browser?

You forgot to call .perform() at the end! Without .perform(), actions remain queued in memory.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Python
Skip