0

I have been trying for a while to complete this task. For context, I have a link called with text "Main Menu" along the top of the page, that when hovered reveals a list of more links. These links can either be clicked, to take you to the main page of that heading, or hovered over, to show more links related to that page.

Right now I have a hover method:

hover = ActionChains(self.browser)
hover.move_to_element((self.browser.find_element_by_link_text(elementId)))
hover.perform()

Where elemendId is the parameter passed to the hover method.

After this hover method is called, I click on link and it works correctly, but if I try to call my hover method twice in a row and then click on the link, it will navigate down to the given menu option, reveal the link that I am wanting to click, and then closes the menu before it has the chance to click on the link.

I have also been looking in to executing a Javascript function to call attempt to hover

self.browser.execute_script("$(\"a:contains('Main Menu')\").hover();")

but I don't have much experience with it and I am unsure if this is anywhere close to correct.

Thanks for your time, Waq

1
  • Are you trying to test the menu or trying to navigate to another page? Jun 30, 2014 at 12:19

2 Answers 2

1

Do you have a single monitor? Is your actual mouse cursor hovering over the page while your automation is running? Sometimes your mouse will mess up the automation that is running. If you have a second monitor, or if you can move your mouse cursor outside of the bounds of the browser window, do you still have the problem?

In javascript there isn't a hover method - there is in jquery, but assuming you don't have jquery - you could do fire the mousemove event like this:

self.browser.execute_script("var evt = document.createEvent('MouseEvent');
evt.initMouseEvent('mousemove', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
arguments[0].dispatchEvent(evt);", 
self.browser.find_element_by_link_text(elementId))

Also note that you can reference an element in the javascript (arguments[0]) and don't need to include the CSS locator in the javascript itself.

3
  • Thanks a lot for the reply, I only have one monitor and I have tried with my mouse outside and inside the window after hearing that that might change the outcome. I found out what was wrong since, but I think I will give your method a go too to see what happens. Thanks again!
    – Waq
    Jul 1, 2014 at 8:13
  • Good to hear, can you share what was wrong so if someone else has the same problem in the future they might learn from it?
    – Sam Woods
    Jul 1, 2014 at 15:46
  • I have added an answer to my question with what worked for me. Thanks again.
    – Waq
    Jul 3, 2014 at 6:58
1

I eventually managed to find a fix for this!

Here is the code that I added:

cap = DesiredCapabilities.INTERNETEXPLORER
cap['requireWindowFocus'] =  True
cap['enablePersistentHover'] = False

I put this just before

self.browser = webdriver.Ie()

Although, this may not be a long term solution as it requires the IE window you are using to be your current selected window, and it actually controls your cursor.

So this would be okay if you were leaving a test to run overnight, but if you have a long test that you want to run in the background you may have to use a virtual machine.

I am going to give Sam's answer a go as that may be a better way to make this work.

Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.