How to do mouse hover using Selenium WebDriver in Firefox 19?
Asked Answered
S

6

17

I have used selenium 2.31.

I have used Actions class for mouse movement. Using this I moved the mouse over a menu and its submenu appeared only for a fraction of second unlike with older version of Firefox .

Because of this issue I cannot select the sub menu using driver.findElement as it throws an exception "element cannot be scrolled into view".

Is there any solution for this?

Steenbok answered 11/3, 2013 at 13:4 Comment(2)
If possible, can you give the link of the application you are testing so that we can debug the issue?Skinnydip
What language have you used? Java, C# or what?Beam
S
32

With the actions object you should first move the menu title, and then move to the popup menu item and click it. Don't forget to call actions.perform() at the end. Here's some sample Java code:

Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.linkText("Menu heading"));
actions.moveToElement(menuHoverLink);

WebElement subLink = driver.findElement(By.cssSelector("#headerMenu .subLink"));
actions.moveToElement(subLink);
actions.click();
actions.perform();
Salvage answered 11/3, 2013 at 22:4 Comment(3)
that's what I needed: ".MoveToElement(menuElement).Perform()" equals ".Hover()"Wastrel
Is there any way by which I can hold that hover for specific time? In my case I am able to hover but submenu then hides very quickly and then web driver not able to find that submenu..Offenseless
@Helping Hands - you can use ClickandHold()Herodotus
I
3

Another way to go about this is to use Selenium's JavaScript Executor to force the style of the element to be displayed.

An Example of this would be along this lines in C#

//Use the Browser to change the display of the element to be shown
 (IJavaScriptExecutor)driver).ExecuteScript("document.getElementById('myId').stlye.display="block");

//navigate to your link that is now viewable 
driver.FindElement(By.Xpath('//LinkPath')).Click(); 

From there, you can find the XPath to your element and use selenium to click on the element. You can cascade this to find children of your main element as well

//(IJavaScriptExecutor)ffbrowser).ExecuteScript("document.getElementById('myId').children[1].children[1].style.display='block'");

Note that this is only possible if you have a hover element that changes the display style when hovered over.

Intermediary answered 26/8, 2013 at 16:30 Comment(0)
D
3

Try this code... It's c sharp code...

//Webelement is the main menu Link
webElement = driver.FindElement(By.XPath("Your element xpath"));
Actions act = new Actions(driver);
        act.MoveToElement(webElement).Perform();//This opens menu list

        System.Threading.Thread.Sleep(5000);//This line will help you to hold menu 
 //This web element is the sub menu which is under main menu
        webElement = driver.FindElement(By.XPath("Sub menu path"));
        act.MoveToElement(webElement).Perform();//This opens menu list
        System.Threading.Thread.Sleep(5000);//Holds menu
    //This web element is the option you have to click
        webElement = driver.FindElement(By.XPath("Path"));
        webElement.Click();
Dodecasyllable answered 13/12, 2013 at 10:24 Comment(1)
Thanks for this. The act.MoveToElement(webElement).Perform(); on the first top menu was critical for my case to open up the hidden menu. I was missing Perform() at the end and just MoveToElement(webElement) was not enough.Stereogram
A
1

This will be helpful if you are using Ruby.

1.First you need to find element by xpath or id.

2.Then use the method action.move_to().perform.

Here is the code:

    hover = WAIT.until{$driver.find_element(:xpath,"xpath")}
    driver.action.move_to(hover).perform
Aspergillosis answered 28/2, 2014 at 11:41 Comment(0)
P
0

This answer helped solve my problem.

My challange was to find a link under a menu option. The link was not visible until I hovered over the Menu.

This crucial part for me was finding out that in addition to hovering over the menu, I next had to hover on the link in order to interact with it.

Personage answered 12/6, 2013 at 3:50 Comment(0)
A
0
List<WebElement> list = driver.findElements(By.xpath("//a"));
        for (int i=0;i<list.size();i++){
        if(list.get(i).getText().equalsIgnoreCase("cacique intimates M"))
            {
    new Actions(driver).moveToElement(list.get(i)).click().build().perform();
    System.out.println("Clicked on Parent Category");
    new Actions(driver).moveToElement(list.get(i)).moveToElement(driver.findElement(By.linkText("SPECIALTY BRAS"))).click().build().perform();
        break;
    }                           
    }
Anapest answered 3/6, 2016 at 21:52 Comment(1)
You would want to explain it? And what it does? What does it do differently than existing accepted answer. By the way, "cacique intimates M" where does it come from? Could not find it in the question.Huebner

© 2022 - 2024 — McMap. All rights reserved.