Browser Actions / Mouse Actions

Mouse actions are nothing but the simulating the mouse events like operation 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 .

Following are few methods present in the ActionChains class in python selenium.

    Mouse Actions :

  • mouseMove
  • dragAndDrop
  • click
  • doubleClick
  • mouseUp
  • mouseDown

  • Keyboard Actions :

  • keyDown
  • keyUp
  • send_keys

perform() is a function which will make the mouse to 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 it's properties like color, highlight, background and font, To perform hove we should use a method called mouseMove() from selenium bindings.

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

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

In below example we are hovering on a sub-menu which has id='sub-menu', sometimes this method is also known as mouseHover()


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 progs\driverserver\chromedriver.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 reveling it sub-options on hover mouse-hover-selenium-python-actions

dragAndDrop 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 issue 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:\PATH\chromedriver.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.

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


def test_click_element(self):
	driver = webdriver.Chrome(executable_path=r'D:\Eclipse progs\driverserver\chromedriver.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 a helper keys and normal like CTRL+A, SHIFT+A, CTRL+SHIFT+delete

Most of the all the keyboard actions are used together. In below example I am trying to set value "A" in search field of Google, but i am passing lower case letter in send_keys with key press 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 progs\driverserver\chromedriver.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

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
You can also share knowledge by Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : We have closed registration for training

Please do email to chercher.tech@gmail.com for any queries

If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
 
Join My Facebook Group
Join Group