27

It seems this is the way to do hover / mouseover in webdriver, at least in the java api:

Actions action = new Actions(driver);
action.moveToElement(element).build().perform();
action.moveByOffset(1, 1).build().perform();

Is this possible in the Python api? The webdriver api docs for python don't seem to mention anything like it. http://selenium.googlecode.com/svn/trunk/docs/api/py/index.html

How is hover / mouseover done in python webdriver?

Purrell
  • 12,461
  • 16
  • 58
  • 70

2 Answers2

39
from selenium.webdriver.common.action_chains import ActionChains


def hover(self):
    wd = webdriver_connection.connection
    element = wd.find_element_by_link_text(self.locator)
    hov = ActionChains(wd).move_to_element(element)
    hov.perform()
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Cola
  • 2,097
  • 4
  • 24
  • 30
  • 1
    How do I close the mouse over popover after opening it? This works for me to open the mouseover popover. But then it stays open and never closes. I tried to find another element on the page and use the move_to_element to move to that other element. No luck with that though! – nids Dec 06 '13 at 18:16
  • @nids, click on an element outside of the mouseover element? – Cola Jun 09 '14 at 18:59
2

I think you are asking for scenarios where we need to click on the item of a drop down list menu. We can automate it in python using Selenium.

In order to perform this action manually, first we need to bring up the drop down list menu by holding mouse over the parent menu. Then click on required child menu from drop down menu displayed.

Using ActionChains class in Selenium WebDriver, we can do this step in same manner as we do manually. The method is described below -

Step 1: Import webdriver module and ActionChains class

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

Step 2: Open Firefox browser and load URL.

site_url = 'Your URL'
driver = webdriver.Firefox()
driver.get(site_url)

Step 3: Create ActionChains object by passing driver object

action = ActionChains(driver);

Step 4: Find first level menu object in page and move cursor on this object using method ‘move_to_element()’. Method perform() is used to execute the actions that we have built on action object. Do the same for all objects.

firstLevelMenu = driver.find_element_by_id("first_level_menu_id_in_your_web_page")
action.move_to_element(firstLevelMenu).perform()
secondLevelMenu = driver.find_element_by_id("second_level_menu_id_in_your_web_page")
action.move_to_element(secondLevelMenu).perform()

Step 5: Click on the required menu item using method click()

secondLevelMenu.click()

Final block of code is like this:

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

site_url = 'Your URL'
driver = webdriver.Firefox()
driver.get(site_url)

action = ActionChains(driver);

firstLevelMenu = driver.find_element_by_id("first_level_menu_id_in_your_web_page")
action.move_to_element(firstLevelMenu).perform()
secondLevelMenu = driver.find_element_by_id("second_level_menu_id_in_your_web_page")
action.move_to_element(secondLevelMenu).perform()

secondLevelMenu.click()

You can replace driver.find_element_by_id() according to your work with any other find_elemnt method available in selenium. Hope It will be helpful for you.

Tanmoy Datta
  • 1,604
  • 1
  • 17
  • 15