protractor icon indicating copy to clipboard operation
protractor copied to clipboard

How to similulate mouse over events such as click a link on mouse over

Open vishalshivnath opened this issue 13 years ago • 18 comments

I have a main menu. If i click on main menu it opens sub menu on mouse over. This sub menu us hidden element. Protractor is clicking on the main menu but not cant simulate the action on mouse over event

Tried below four ways to simulate mouse hover action

  1. driver.executeScript("document.getElementById('dBysuspect typeLabel')[0].mouseover();");
  2. driver.executeScript("return ptor.By.id('dBysuspect typeLabel').hover();");
  3. reviewProviderPage.reviewProvidersBySuspect.hover();
  4. driver.executeScript("reviewProviderPage.reviewProvidersBySuspect.hover():");

Got below error. Message: Type Error: Object [object Object] has no method 'hover'

vishalshivnath avatar Oct 11 '13 10:10 vishalshivnath

You'll need to use an action sequence for hovering:

ptor.actions().
    mouseMove(ptor.findElement(protractor.B.id('foo'))).
    perform();

See https://code.google.com/p/selenium/source/browse/javascript/webdriver/actionsequence.js for the full actions API.

juliemr avatar Oct 11 '13 21:10 juliemr

Thanks it working, hence closing this ticket

vishalshivnath avatar Oct 15 '13 06:10 vishalshivnath

This doesn't work for me. :/ For the following code:

browser.actions().mouseMove($('cbn-slider')).perform();

I get an error:

   Message:
     TypeError: Object [object Object] has no method 'toWireValue'
   Stacktrace:
     TypeError: Object [object Object] has no method 'toWireValue'
    at null.<anonymous> (*/test/e2e/e2e-spec.defs.js:57:31)
    at */node_modules/protractor/jasminewd/index.js:54:12
    at wrapper [as _onTimeout] (timers.js:252:14)
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
==== async task ====
    at null.<anonymous> (*/node_modules/protractor/jasminewd/index.js:53:12)
    at null.<anonymous> (*/node_modules/protractor/node_modules/minijasminenode/lib/async-callback.js:45:37)

mgol avatar Jan 24 '14 18:01 mgol

OK, I found the answer here: https://github.com/angular/protractor/issues/123#issuecomment-32619455 Instead of $('cbn-slider') I need to write $('cbn-slider').find(). Seems a little unintuitive and documentation here is sparse at best.

mgol avatar Jan 24 '14 18:01 mgol

Hey @mzgol thanks for the .find() update :+1:

elgalu avatar Feb 07 '14 14:02 elgalu

Yep, was also stuck until I've stuble upon this issue.

I would suggest adding something about this in the doc. If you tell me where it's best to put it, I'm keen to do a PR

ffesseler avatar May 17 '14 10:05 ffesseler

Hi, I am having a drop down menu which shows up on mouse over and hides on mouse out, The below code fire a mouse out event instantly so the menu hides immediately. So all you can see is a flicker on the screen. Any help would be appreciated.

ptor.actions(). mouseMove(ptor.findElement(protractor.By.id('id_element'))).perform();

vishaljoshi avatar Jun 25 '14 10:06 vishaljoshi

@vishalshivnath I have recently come across the same issue as you, so created this ticket:

https://github.com/angular/protractor/issues/1034

ghost avatar Jul 04 '14 16:07 ghost

HI,

I am new to selenium, i working with menu, need to click sub menu using selenium webdriver, could anyone help with tat :(

Rajgowtham avatar Jul 14 '14 09:07 Rajgowtham

I'm using the action sequence that @juliemr suggested in order to perform a hover over a button. It's working for me in FireFox and Chrome, but it's throwing the following error when I run my tests in Safari. Any ideas how to work around this? I've been searching the web for a solution, but the ones I've found so far are not working out for me when I try them in elementexplorer..

Example page I've looked into: https://code.google.com/p/selenium/issues/detail?id=2067 Perhaps I need a bit of time to grasp the code in #57 and #60, but I'm also wondering if this issue is being looked at, whether by the Protractor team or the WebDriver team..

UnknownError: Unknown command: mouseMoveTo (WARNING: The server did not provide any stacktrace information)
[safari #3] Command duration or timeout: 4 milliseconds
[safari #3] Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:32:38'
[safari #3] System info: os.name: 'Windows Server 2008 R2', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_60'
[safari #3] Session ID: null
[safari #3] Driver info: org.openqa.selenium.safari.SafariDriver
[safari #3] Capabilities [{platform=WINDOWS, javascriptEnabled=true, cssSelectorsEnabled=true, secureSsl=true, browserName=safari, takesScreenshot=true, version=5.1.7}]
[safari #3]    Stacktrace:
[safari #3]      UnknownError: Unknown command: mouseMoveTo (WARNING: The server did not provide any stacktrace information)
[safari #3] Command duration or timeout: 4 milliseconds
[safari #3] Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:32:38'
[safari #3] System info: os.name: 'Windows Server 2008 R2', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_60'
[safari #3] Session ID: null
[safari #3] Driver info: org.openqa.selenium.safari.SafariDriver
[safari #3] Capabilities [{platform=WINDOWS, javascriptEnabled=true, cssSelectorsEnabled=true, secureSsl=true, browserName=safari, takesScreenshot=true, version=5.1.7}]
[safari #3] ==== async task ====
[safari #3] mouseMove
[safari #3]     at Array.forEach (native)
[safari #3] ==== async task ====
[safari #3] ActionSequence.perform
[safari #3]     at hover (maxbase.js:149:7)
[safari #3]     at showLoginMenu (maxhome.js:94:12)
[safari #3]     at logout (maxhome.js:8:14)
[safari #3]     at null.<anonymous> (home.test.js:93:14)
[safari #3] ==== async task ====
[safari #3] Asynchronous test function: it()
[safari #3] Error
[safari #3]     at testSigningOut (home.test.js:91:2)
[safari #3]     at null.<anonymous> (p1.spec.js:30:14)
[safari #3]     at Object.<anonymous> (p1.spec.js:7:1)

aricearice avatar Jul 17 '14 19:07 aricearice

@aricearice - Were you able to find out the solution for your problem. I have stumbled upon same issue in Safari.

abhagupta avatar Feb 05 '15 21:02 abhagupta

Same here, I've stepped on this issue too... Did anyone find a workaround?

Thanks

bmsoko avatar May 20 '15 19:05 bmsoko

For Safari issue, see also #1526. As mentioned by @aricearice, comments 57 & 60 in the referenced Selenium issue on Google Code should have the workaround.

Basically, the workaround is to execute JS code in the browser to simulate mouse events (rather than use native events which SafariDriver does not yet support). Javascript based mouse over or hover, something you can search online about to get a better understanding of it (as well as more code samples), if comments 57 & 60 are not easy to decipher.

Also, if one needs javascript based drag & drop, see #1526, where I mention another reference to JS code snippet for that.

Also, if one wants mouse click instead of hover or drag & drop, can do that too. I have a blog post about all these JS simulations here:

https://autumnator.wordpress.com/2013/02/09/javascript-is-your-ally-for-selenium-webdriver/

daluu avatar Jul 15 '15 05:07 daluu

I am unable to hover over an element( hyper link). I observed that when I placed my physical mouse pointer on the browser( launched through automation ) the element did not hovered but once I moved the physical pointer outside of the browser as soon as run gets triggered, the hovering action is passing successfully. I am running the script in chrome browser using protractor

The code snippet that I used is

browser.sleep( 8000 ); var ele = element( by.xpath( '//tf-infobox//[normalize-space(.)="Error 1"]/following-sibling:://a' ) ); browser.actions().mouseMove( ele ).perform(); browser.sleep( 5000 );

Additional Info: I can able to click the element but hovering is not possible. Chrome-driver version: 2.21

Can someone help me out to solve my problem.

Thanks!

csanthoshi avatar Aug 25 '16 11:08 csanthoshi

I second @csanthoshi . Is there a way to fire it only from script without the interference of the physical mouse?

latobibor avatar Nov 03 '16 14:11 latobibor

I sort of discovered a workaround to the mouse hover issue on chrome by accident. If we chain the mouseMove() method twice , it works.

Code that doesn't work on chrome: browser.actions.mouseMove(element).click().perform();

Code with workaround(which works) browser.actions.mouseMove(element).mouseMove(element).click().perform();

amod-thakur avatar Nov 16 '18 19:11 amod-thakur

You'll need to use an action sequence for hovering:

ptor.actions().
    mouseMove(ptor.findElement(protractor.B.id('foo'))).
    perform();

See https://code.google.com/p/selenium/source/browse/javascript/webdriver/actionsequence.js for the full actions API.

Hi @juliemr Above code is working fine in chrome. But i need to run my script for firefox and this code is failing in firefox browser. Below is the error i am facing while executing same code on firefox

**

UnknownCommandError: POST /session/24e1787f-7623-4690-b9a9-92ba32c552e1/moveto did not match a known command

** Please help me to solve this issue

vijayjdk avatar Jul 10 '19 09:07 vijayjdk

almost 10 years later I ran into the same situation. And I ended up creating a very simple solution

A function at (mouseenter)="myFunc($event)" in element and then

myFunc(event: any){ event.target.click() }

Done, no pain

viniciusrad avatar Dec 29 '22 20:12 viniciusrad