For accessing mouse hover menu we need to use ‘ActionChains’ function.

First import ‘ActionChains’

from selenium.webdriver.common.action_chains import ActionChains

In our example we are going to access Drupal admin menu Content » Add content » Basic page menu item.

Here the xpaths:

Content Menu item:

cls.content_menu_xpath = "//li[contains(@class, 'admin-menu-toolbar-category expandable')]/a[contains(@href, '/admin/content')]"

Add Content:

cls.add_content_menu_xpath = "//li[contains(@class, 'admin-menu-toolbar-category expandable')]/ul[contains(@class, 'dropdown')]/li[contains(@class, 'expandable')]/a[contains(@href, '/node/add')]"

Add Basic Page:

cls.add_basic_page_xpath = "//ul[contains(@id, 'admin-menu-menu')]/li[2]/ul/li[1]/ul/li[2]/a"

Now hover the menu and click on the Add Basic page link:

def admin_hover_menu(self):

    content_menu_hover = self.driver.find_element_by_xpath(self.content_menu_xpath)
    hover_content = ActionChains(self.driver).move_to_element(content_menu_hover)
    hover_content.perform()
    add_content_hover = self.driver.find_element_by_xpath(self.add_content_menu_xpath)
    hover_add_content = ActionChains(self.driver).move_to_element(add_content_hover)
    hover_add_content.perform()

    #Click on hover menu item (Content > Add content > Basic Page)
    self.assertTrue(self.is_element_present(By.XPATH, self.add_basic_page_xpath))
    self.driver.find_element_by_xpath(self.add_basic_page_xpath).click()

    #verifiy create basic page
    self.assertTrue(self.is_element_present(By.XPATH, self.basic_page_title_xpath))

That’s all!

Advertisements