Mouse Actions in Selenium python

Mouse actions are nothing but simulating the mouse events like operations performed using Mouse. In Selenium Python, handling Mouse & keyboard events and mouse events (including actions such as Drag and Drop or clicking multiple elements With Control key) are done using the ActionChains API.

Mouse Actions :
    • mouseMove
    • dragAndDrop
    • click
    • doubleClick
    • mouseUp
    • mouseDown
Keyboard Actions :
  • keyDown
  • keyUp
  • send_keys

perform() is a function that will make the mouse perform an operation on the webpage, if you don't use perform() function then ActionChains don't have any effect on the webpage.

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.

We can combine a few mouse and keyboard action methods as per our wish.

mouseMove / hover in python selenium

Sometimes we may need to hover on an element to see its properties like color, highlight, background, and font; to perform hover, we should use a method called mouseMove() from selenium bindings.

Wwe will not stop with verifying element properties; we also have elements on the HTML page that will be revealed only when you hover on them, for example, sub-menu.

With the object of the actions, you should first move to the menu element, and then move to the submenu item and click it or perform whatever action you wish.

In the below example we are hovering on a sub-menu that has id='sub-menu', sometimes this method is also known as mouse hover

import unittest
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

class Test(unittest.TestCase):
    def test_move_to_element(self):
        driver = webdriver.Chrome(executable_path=r'D:Eclipse progsdriverserverchromedriver.exe');
        driver.implicitly_wait(30)
        driver.get("https://chercher.tech/practice/practice-pop-ups-selenium-webdriver");

        # find the element
        menu = driver.find_element_by_id("sub-menu")
        #Create the object for Action Chains
        actions = ActionChains(driver)
        actions.move_to_element(menu)
        # perform the operation on the element
        actions.perform()

if __name__ == "__main__":
    unittest.main()


Menu revealing its sub-options on hover.
mouse-hover-selenium-python-actions

Drag and drop in selenium python

dragAndDrop() drags source element into the target element or to the place, dragAndDrop() method accepts two parameters,

  1. Source: which you want to drag,
  2. Target: where you want to drag

This has some issues with HTML 5 language, so if it is not working, then it is because of it.

import unittest
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

class Test(unittest.TestCase):
    def test_move_to_element(self):
        driver = webdriver.Chrome(executable_path=r'D:PATHchromedriver.exe');
        driver.implicitly_wait(30)
        driver.get("https://chercher.tech/practice/practice-pop-ups-selenium-webdriver");

        # perform drag and drop
        source = driver.find_element_by_id("drag1")
        target = driver.find_element_by_id("div2")
        # Create the object for Action Chains
        actions = ActionChains(driver)
        actions.drag_and_drop(source, target)
        # perform the operation on the element
        actions.perform()

if __name__ == "__main__":
    unittest.main()

click in mouse action with python selenium bindings

click() method clicks a given element, or at a given location, it is always better to click an element than clicking a location.

The location of the elements may change based on the display size of your computer.

def test_click_element(self):
	driver = webdriver.Chrome(executable_path=r'D:Eclipse progsdriverserverchromedriver.exe');
	driver.implicitly_wait(30)
	driver.get("https://chercher.tech/practice/practice-pop-ups-selenium-webdriver");

	element_to_click = driver.find_element_by_id("alert")
	target = driver.find_element_by_id("div2")
	#Create the object for Action Chains
	actions = ActionChains(driver)
	actions.click(element_to_click)
	# perform the operation on the element
	actions.perform()

click-mouse-action-selenium-python

doubleClick using mouse actions in python

doubleClick() method simulates the double click of the mouse, we have to pass the element that to be double-clicked.

driver.get("https://chercher.tech/practice/practice-pop-ups-selenium-webdriver");
element_to_double_click = driver.find_element_by_id("double-click")
target = driver.find_element_by_id("div2")
# Create the object for Action Chains
actions = ActionChains(driver)
actions.double_click(element_to_double_click)
# perform the operation on the element
actions.perform()

double-click-selenium-python-mouse-actions

keyUp & keyDown & send_keys in python

keyUp and KeyDown methods are used to press keyboard keys in python selenium with ActionChains API.

These methods will be useful if you want to press helper keys and normal like CTRL+A, SHIFT+A, CTRL+SHIFT+delete.

Most of all, the keyboard actions are used together. In the below example, I am trying to set value "A" in the search field of Google, but I am passing lower case letter in send_keys with the keypress of Shift

import unittest
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions.interaction import KEY
from selenium.webdriver.common import keys

class Test(unittest.TestCase):
    def test_move_to_element(self):
        driver = webdriver.Chrome(executable_path=r'D:Eclipse progsdriverserverchromedriver.exe');
        driver.implicitly_wait(30)
        driver.get("https://google.com");

        # perform drag and drop
        search_bar = driver.find_element_by_name("q")
        #Create the object for Action Chains
        actions = ActionChains(driver)
        actions.click(search_bar)
        actions.key_down(keys.Keys.SHIFT)
        actions.sendKeys("a")
        actions.key_up(keys.Keys.SHIFT)
        # perform the operation on the element
        actions.perform()

if __name__ == "__main__":
    unittest.main()

keydown-keyup-sendkeys-selenium-python-keyboard-actions

About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • Reply