5 Best Ways to Perform Mouse Hover Action in Selenium Python

Rate this post

πŸ’‘ Problem Formulation: Automating web interactions often involves simulating mouse movements like hover actions. In Selenium Python, achieving mouse hover can enable testing of dropdowns or tooltips – user interface elements that commonly rely on hover events. The goal is to move the mouse over a specific webpage element and trigger any resulting JavaScript events without a click. For instance, hovering over a menu item may display a submenu that needs to be tested.

Method 1: Using ActionChains

ActionChains are a way to automate low-level interactions such as mouse movements, mouse button actions, key press, and context menu interactions. This can be used for performing complex actions like drag-and-drop and mouse hover. The ActionChains class comes with a method called move_to_element() which moves the mouse to the middle of an element.

Here’s an example:

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://example.com")

hover_element = driver.find_element(By.ID, "menu")
ActionChains(driver).move_to_element(hover_element).perform()

Output: The mouse cursor moves over the menu element, potentially triggering any JavaScript events associated with the hover.

In this snippet, we initiate the Chrome WebDriver, navigate to “https://example.com”, and locate an element with the ID “menu”. We then create an ActionChains object and call its move_to_element() method with the located element as a parameter. The perform() method at the end executes the action chain.

Method 2: Setting Mouse Position with pyautogui

Beyond Selenium, the Python library pyautogui can be used to control the mouse and keyboard. It’s a cross-platform GUI automation module that programmatically controls the mouse & keyboard. Specifically, the moveTo() function can position the mouse over a given element by screen coordinates.

Here’s an example:

import pyautogui
import time

# Assuming the button is at (200, 200) on the screen.
button_coordinates = (200, 200)

# Giving time to open the browser and navigate to the page.
time.sleep(5)

# Moves the mouse to the coordinates on the screen.
pyautogui.moveTo(button_coordinates)

# It's often good practice to pause briefly to allow UI to react.
time.sleep(1)

Output: The mouse cursor moves to the screen coordinates (200, 200).

After importing pyautogui, we give time for the user to manually navigate to the webpage in the browser. The moveTo() function is then called with the coordinates of the screen where the hover action should occur. The following sleep just ensures there’s time for any UI elements to respond to the hover event.

Method 3: Hovering with JavaScript Execution

Another approach is to execute JavaScript within the page to trigger a hover event programmatically. The Selenium WebDriver can execute JavaScript code with the execute_script() method by passing a string of JavaScript code.

Here’s an example:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://example.com")

hover_element = driver.find_element(By.ID, "menu")
driver.execute_script("arguments[0].hover()", hover_element)

Output: The JavaScript triggers a hover event on the menu element.

Once WebDriver has loaded the website and an element to be hovered is located, the execute_script() method is invoked. The JavaScript code "arguments[0].hover()" is passed along with the element as an argument, which should induce the hover state.

Method 4: Explicitly Waiting for Hover Elements

Explicit waits in Selenium allow the code to halt for a certain condition to be met before proceeding. This can be useful when waiting for a hover action to produce a visible change, like displaying a submenu. It uses classes like WebDriverWait and expected conditions provided by expected_conditions.

Here’s an example:

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://example.com")

wait = WebDriverWait(driver, 10)
hover_element = wait.until(EC.visibility_of_element_located((By.ID, "menu")))
ActionChains(driver).move_to_element(hover_element).perform()

Output: After ensuring the menu element is visible, a mouse hover action is performed on it.

The code employs an explicit wait to verify that the menu element is visible on the webpage using WebDriverWait. Only after this condition is met, an ActionChains object is used to hover over the element and perform() the action.

Bonus One-Liner Method 5: Quick Hover with ActionChains

If you prefer condensing actions into as few lines as possible, you can hover over an element with just one line using ActionChains.

Here’s an example:

ActionChains(driver).move_to_element(driver.find_element(By.ID, "menu")).perform()

Output: A concise hover action is initiated over the menu element.

The code snippet aims for brevity, chaining the methods together in one line without storing the ActionChains object or the web element in a variable.

Summary/Discussion

Method 1: ActionChains. Provides precision with browser interactions. However, requires element locatable by Selenium.
Method 2: pyautogui. Good for simple mouse positioning by screen coordinates, not tied to a web page’s structure. Less accurate for dynamic content.
Method 3: JavaScript Execution. Precise control over page scripts and may be necessary for some complex interactions. May be blocked by certain JavaScript environments.
Method 4: Explicit Waits. Useful for ensuring that elements are ready before interaction. Adds complexity and code length.
Bonus Method 5: Quick Hover with ActionChains. Offers the most concise code, but can sacrifice readability and maintainability.