0

I am trying to hover over an element but looks like the hover isn't working as expected. Example site: https://www.covergirl.com/en_us/

I am trying to hover over the 3rd menu item(FACE).

find('.global-header__list-link', wait: 5, match: :first, text: 'FACE').hover

This seems to hover the element as I can the item being underlined but the menu doesn't open.

1 Answer 1

1

I'm assuming you're using Chrome with selenium.

You can see that the hover state is actually being set since the word "FACE" becomes underlined when you call hover on the element. What doesn't work is whatever event the menu is relying on isn't being generated. Without digging too far I'd guess that event is the mouseenter event because chromedriver has a known issue where it doesn't generate mouseenter or mouseleave events when moving to an element (but does generate mouseover events). If you use Firefox with selenium, instead of Chrome, you'll see that the menu works fine there.

If you need to stay on Chrome you should be able to workaround the issue with something like

el = find('.global-header__list-link', wait: 5, match: :first, text: 'FACE').hover
execute_script("
  var evt = new MouseEvent('mouseenter', { bubbles: false, cancelable: true, view: window });
  arguments[0].dispatchEvent(evt);
", el)
Sign up to request clarification or add additional context in comments.

6 Comments

This doesn't work for me unfortunately. I did remove the .hover from the el above. FYI this is the JS code that I picked from ChromeDev tools if it helps if (event.type === 'mouseenter') { this.state.menuOpen = true; } else if (event.type === 'mouseleave') { this.state.menuOpen = false; } else { this.state.menuOpen = !this.state.menuOpen; } I am running the latest chromedriver version.
Why did you remove the hover? That's still necessary, and should return the element unless you're running an old version of Capybara. If you are running an old versions upgrade Capybara. gist.github.com/twalpole/d9b65af23ad081ebfe956de84a7297b8 is a gist with the most basic code that opens the "Face" menu and clicks on the "Foundation" sub menu and then verifies the FOUNDATION header is now on the page. It works fine for me. If it "doesn't" work for you I need more details about exactly what doesn't work, exact error, etc
Also run bundle exec chromedriver --version and confirm that is what you expect.
That's strange. When I ran your script the first time it worked but when I ran again it doesn't. I observed this yesterday as well. I am on latest chromedriver release 2.31 and latest capybara 2.15.1 on ruby 2.4.1. It simply underlines the FACE link like it does with hover and nothing else. Does it work consistently for you?
@Rahul Works consistently for me as long as I don't change focus to the Chrome window while it's running (which will screw with the mouse position and cause the menu to close). The fact it doesn't for you implies either you're not actually on chromedriver 2.31 (did you run the version check with bundle exec?) or it's a timing issue. Try inserting a short sleep before the execute_script.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.