ActionChains In Selenium

Selenium is widely used in web applications automation and many a times there is a need for user interaction requiring mouse and keyboard actions.So how do we interact with the browser using Selenium test automation?The simple answer is through ActionChain API.In this blog I have attempted to show the implementation of some mouse and keyboard actions with an example.


ActionChains are a way to automate low level interactions such as mouse movements, mouse button actions, key press, and context menu interactions. This is useful for doing more complex actions like mouse over and drag and drop.


Generate user actions

When you call methods for actions on the ActionChains object, the actions are stored in a queue in the ActionChains object. When you call perform(), the events are fired in the order they are queued up.


ActionChains can be used in a chain pattern:


menuTest = driver.find_element_by_css_selector(".test")

hidden_submenuTest = driver.find_element_by_css_selector(".test #submenu1") ActionChains(driver).move_to_element(menuTest).click(hidden_submenuTest).perform()

Or actions can be queued up one by one, then performed.:

menuTest = driver.find_element_by_css_selector(".test")

hidden_submenuTest = driver.find_element_by_css_selector(".test #submenu1")

actions = ActionChains(driver)

actions.move_to_element(menuTest)

actions.click(hidden_submenuTest)

actions.perform()


Either way, the actions are performed in the order they are called, one after another.


The various methods provided by Selenium for ActionChain API are:


  • click(on_element=None) Clicks an element.

  • click_and_hold(on_element=None)Holds down the left mouse button on an element.

  • context_click(on_element=None)Performs a context-click (right click) on an element.

  • double_click(on_element=None)Double-clicks an element.

  • drag_and_drop(source, target)Holds down the left mouse button on the source element,then moves to the target element and releases the mouse button.

  • drag_and_drop_by_offset(source, xoffset, yoffset) Holds down the left mouse button on the source element,then moves to the target offset and releases the mouse button.

  • key_down(value, element) Sends a key press only, without releasing it.Should only be used with modifier keys (Control, Alt and Shift).

  • key_up(value, element)Releases a modifier key.

  • move_by_offset(xoffset, yoffset)Moving the mouse to an offset from current mouse position.

  • move_to_element(to_element)Moving the mouse to the middle of an element.

  • move_to_element_with_offset(to_element, xoffset, yoffset)Move the mouse by an offset of the specified element.Offsets are relative to the top-left corner of the element.

  • pause(seconds)Pause all inputs for the specified duration in seconds

  • perform()Performs all stored actions.

  • release(on_element)Releasing a held mouse button on an element.

  • reset_actions()Clears actions that are already stored locally and on the remote end

  • send_keys(*keys_to_send) Sends keys to current focused element.

  • send_keys_to_element(element, *keys_to_send) Sends keys to an element.

How to use Action Chains in Selenium ?



I hope you understood the importance of ActionChains in Selenium and the flexibility it provides to mimic user interaction.Happy Testing!

22 views0 comments

Recent Posts

See All

Headless Browser in Python

What is a headless browser? A headless browser can access any website but unlike normal browsers (which you currently use) nothing will appear on the screen. Everything is done on the backend side inv