Mouse Hover Over

In most of the websites today, there are a lot of pages, which lists categories and sub categories. On the UI of these websites, most of these are arranged in a manner of Hover-overs.

Let me give you an example- head over to Flipkart, and click on a category. You will see an extended option, containing a lot of sub-menus. This are enabled only when you hover over a mouse on the parent category or parent menu.

Workspace 1_090

Now,since it is presence in most of websites, it is essence of a web tester to know how to automate it. Thankfully, Selenium gives us the flexibility or freedom to automate these kind of scenarios.

Using the ActionChains class of the Selenium Python bindings (in Java it is Actions class), we can automate this scenario.

We would use the move_to_element( ) method to move to corresponding elements in the web page.

First we would get the xpath’s of the corresponding web elements and then we use the  move_to_element( ) method to move to corresponding element.

So we would go in this manner

SOAP UI -> Introduction to HTML -> Introduction to SOAP/REST/XML-RPC 

import unittest
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
chrome_path = "/home/rahul/Documents/SeleniumPy/chromedriver"

class ActionsTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome(chrome_path)

    def test_Actions(self):
        driver = self.driver
        driver.maximize_window()
        driver.get('https://qatechnicals.wordpress.com/')

        print(driver.title)

        element1 = driver.find_element_by_xpath("//*[@id='menu-item-593']/a")
        element2 = driver.find_element_by_xpath("//*[@id='menu-item-637']/a")
        element3 = driver.find_element_by_xpath("//*[@id='menu-item-671']/a")

        hoverover =ActionChains(driver).move_to_element(element1).move_to_element(element2).move_to_element(element3).click().perform()

        WebDriverWait(driver, 30).until(expected_conditions.title_contains("XML"))

        print(driver.title)

        driver.back()

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

Here, I have taken the example of one of my blogs, QATechnicals, to automate a simple hover over.

Element 1 represents the SOAP UI home page xpath, Element 2 represents the corresponding element (Introduction to XML) and Element 3 represents the final page that we want to go to.