Skip to content

Instantly share code, notes, and snippets.

@umaar
Created February 17, 2014 14:05
Show Gist options
  • Save umaar/9051143 to your computer and use it in GitHub Desktop.
Save umaar/9051143 to your computer and use it in GitHub Desktop.
Hover over an element in WebDriverJS
/*
* Hover over an element on the page
*/
(function() {
"use strict";
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();
driver.manage().window().setSize(1280, 1000).then(function() {
driver.get('http://www.shazam.com/discover/track/58858793');
driver.findElement(webdriver.By.css('.ta-tracks li:first-child')).then(function(elem){
driver.actions().mouseMove(elem).perform();
driver.sleep(5000);
driver.quit();
});
});
}());
@dr4ke1990
Copy link

Just wanted to thank you for making this available. I couldn't work out how to do the 'hover' function and the window().setSize() function is also very helpful.

Thank you :-)

@RuthvikReddy
Copy link

Thank You Umaar for making this available. I was struggled to figure out how can I 'hover' on an element and click on. But with this it makes it easy.

@birdage
Copy link

birdage commented Dec 12, 2016

👍

@farahkadri
Copy link

but what if the element does not have id. and href

@ryancat
Copy link

ryancat commented Mar 27, 2018

actions is not working for chrome driver SeleniumHQ/selenium#5428

@adiwon9555
Copy link

adiwon9555 commented May 11, 2018

You can use this code for mouse hover in selenium javascript:-
const actions = driver.actions({bridge: true}); var elem=await driver.findElement(By.id("myId")); await actions.move({duration:5000,origin:elem,x:0,y:0}).perform();

This code has to be inside an async function as await is used, or else to use with promise, this code might help:-

const actions = driver.actions({bridge: true}); driver.findElement(By.id("myId")).then((elem)=>{ actions.move({duration:5000,origin:elem,x:0,y:0}).perform(); });
hope it helps....

@bom6
Copy link

bom6 commented May 11, 2018

Can we specify x and y in %?

@adiwon9555
Copy link

x and y will be taken as pixels by default..

@navudaiyappan
Copy link

async function moveTheCursor(selector){
const actions = this.driver.actions({bridge: true});
var elem=await getElementBySelector.call(this, selector);
await this.actions.move({duration:5000,origin:elem,x:0,y:0}).perform();
}
When I try this one I am getting "Cannot read property 'move' of undefined" . Can anyone please help me?

@AlexeyGorobtsov
Copy link

const actions = require('selenium-webdriver/lib/actions');
const LegacyActionSequence = actions.LegacyActionSequence;
const webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;

let driver = new webdriver.Builder()
.forBrowser('chrome')
.build();

driver.get('http://www.google.com')
.then(() => driver.findElement(By.id('gs_ok0')).then(elem => {
new LegacyActionSequence(driver).mouseMove(elem).perform();
}));

https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/actions_exports_LegacyActionSequence.html

@qtcoder999
Copy link

You can use this code for mouse hover in selenium javascript:-
const actions = driver.actions({bridge: true}); var elem=await driver.findElement(By.id("myId")); await actions.move({duration:5000,origin:elem,x:0,y:0}).perform();

This code has to be inside an async function as await is used, or else to use with promise, this code might help:-

const actions = driver.actions({bridge: true}); driver.findElement(By.id("myId")).then((elem)=>{ actions.move({duration:5000,origin:elem,x:0,y:0}).perform(); });
hope it helps....

This works amazingly well in Chrome! Thanks for the help.

@pgupt
Copy link

pgupt commented Jul 7, 2020

You can use this code for mouse hover in selenium javascript:-
const actions = driver.actions({bridge: true}); var elem=await driver.findElement(By.id("myId")); await actions.move({duration:5000,origin:elem,x:0,y:0}).perform();

This code has to be inside an async function as await is used, or else to use with promise, this code might help:-

const actions = driver.actions({bridge: true}); driver.findElement(By.id("myId")).then((elem)=>{ actions.move({duration:5000,origin:elem,x:0,y:0}).perform(); });
hope it helps....

This works. Thanks.

@ghou1337
Copy link

ghou1337 commented Aug 4, 2022

<3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment