in webdriver Actions mouseover ~ read.

Click menu item with mouse hover using WebDriver

Today I needed to select menu item from menu that couldn't be clicked, but had to be hovered to show all items. WebDriver has nice Actions class that easily allows you to do that :

public void selectFromDropDownWithHover(String menuTitle, String menuItem, String locatorType)  
throws Exception {  
        Actions actions = new Actions(manager.getDriver());
        //first we find the menu title 
        //on which we will hover mouse cursor
       WebElement menuHoverLink = findElementByLocator(locatorType, menuTitle);
       //now we hover it with mouse
       actions.moveToElement(menuHoverLink).perform();
       //let's find our menu item
       WebElement menuItemElement = findElementByLocator(locatorType, menuItem);
       actions.moveToElement(menuItemElement);
       actions.click().build().perform();
    }

You can copy this code also from my gist

In case i have findElementByLocator method that simply checks which type of locator i want to use and then returns me WebElement object. It looks next way :

public WebElement findElementByLocator(String locatorType,String locator)  
throws Exception {  
    WebElement element = null;
            if (locatorType.equals("id")) {
                element = driver.findElement(By.id(locator));
            } else if (locatorType.equals("css")) {
                element = driver.findElement(By.cssSelector(locator));
            } else if (locatorType.equals("xpath")) {
                element = driver.findElement(By.xpath(locator));
            } else if (locatorType.equals("linkText")) {
                element = driver.findElement(By.linkText(locator));
            }else if (locatorType.equals("name")) {
                element = driver.findElement(By.name(locator));
            } else {
                throw new Exception("Invalid type 
                of locator was passed.");
            }
        return element;
    }

In case if you have more then 1 level of menu, then instead of menu item locator string value you can pass array of strings and then go through each of it and continue element.moveToElement().perform() until you get to the last element. That one you simply click and that's it :)

Enjoy.

comments powered by Disqus
comments powered by Disqus