- How to perform mouse movement to an element in Selenium with python?
- Syntax
- Syntax
- Example
- Simulate mouseover in Selenium (Python)
- Setting up Selenium
- Triggering mouseover in Selenium
- Mouse & Keyboard actions Python Selenium
- mouseMove / hover in python selenium
- Drag and drop in selenium python
- click in mouse action with python selenium bindings
- doubleClick using mouse actions in python
How to perform mouse movement to an element in Selenium with python?
We can perform mouse movement to an element in Selenium with the help of Action Chains class. These classes are generally used for automating interactions like context menu click, mouse button actions, key press and mouse movements.
These types of actions are mainly common in complex scenarios like drag and drop and hovering over an element on the page. The methods of the Action Chains class are utilized by advanced scripts. We can manipulate DOM with the help of Action Chains in Selenium.
The action chain object implements the ActionChains in the form of a queue and then executes the perform() method. On calling the method perform(), all the actions on action chains will be performed.
The method of creating an Action Chain object is listed below −
- First we need to import the Action Chain class and then the driver will be passed as an argument to it.
- Now all the operations of action chains can be done with the help of this object.
Syntax
Syntax for creating an object of Action Chains −
from selenium import webdriver
# import Action chains from selenium.webdriver import ActionChains # create webdriver object driver = webdriver.Firefox() # create action chain object action = ActionChains(driver)
After creating an object of Action Chains, we can perform numerous operations one by one like a chain which is queued.
move_to_element() — This method performs movement of mouse to the middle of the element on the page.
Syntax
Where args is the element to move to.
#element source = driver.find_element_by_id("name") #action chain object action = ActionChains(driver) # move to element operation action.move_to_element(source).click().perform()
Example
Code Implementation for moving to an element.
from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.keys import Keys #browser exposes an executable file #Through Selenium test we will invoke the executable file which will then #invoke actual browser driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://www.tutorialspoint.com/about/about_careers.htm") #to refresh the browser driver.refresh() # identifying the source element source= driver.find_element_by_xpath("//*[text()='Company']"); # action chain object creation action = ActionChains(driver) # move to the element and click then perform the operation action.move_to_element(source).click().perform() #to close the browser driver.close()
Simulate mouseover in Selenium (Python)
Sometimes, it’s not entirely convenient to scrape data from the web: sometimes one needs to hover the mouse pointer over a particular element for another one to be rendered. In this blog post, we’ll simulate a mouseover in Selenium and Python.
To mimic a mouseover event to trigger the display of a DOM element (e.g. a tooltip), we will use Selenium’s ActionChains class. It is the preferred way to create a sequence of actions like hovering over an element and clicking another one. From the Selenium (unofficial) documentation:
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.
Setting up Selenium
In this first chunk of code, we load the necessary classes to run Chrome and to achieve our goal of hovering and clicking: obviously, the webdriver and the ChromeOptions class. Finally, the ActionChains class.
from selenium import webdriver from selenium.webdriver import ChromeOptions from selenium.webdriver.common.action_chains import ActionChains options = ChromeOptions() options.add_argument("--start-maximized") chrome_driver = webdriver.Chrome(options=options)
Triggering mouseover in Selenium
This next chunk is where it gets interesting. We select the necessary items using CSS selectors.
- First, we select the element that needs to be hovered. (elem_hover)
- Second, we select the element that needs to be clicked. (elem_click)
elem_hover = chrome_driver.find_element_by_css_selector('#your-element') elem_click = chrome_driver.find_element_by_css_selector('#your-element #sub-element')
In this final chunk of code, we create the sequence via an ActionsChains object which we named actions. First we will move the mouse to our elem_hover element to trigger the mouseover event that will render our elem_click element, which we will click in a second step.
The perform() method triggers the sequence.
actions = ActionChains(chrome_driver) actions.move_to_element(elem_hover) actions.click(elem_click) actions.perform()
Congratulations, we’ve triggered a mouseover in Selenium.
Mouse & Keyboard actions Python Selenium
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 :
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.
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 , 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.
Drag and drop in selenium python
dragAndDrop() drags source element into the target element or to the place, dragAndDrop() method accepts two parameters,
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()
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()
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()