Implement mouse hover using JavascriptExecutor

7,248 views
Skip to first unread message

Mattayi

unread,
Mar 10, 2012, 9:39:32 AM3/10/12
to webdriver
Hi,

Is it possible to implement mouse hover using JavascriptExecutor. The
code i am using is the following.

WebElement element = driver.findElement(By.xpath(Locator));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].mouseOver()", element);

Note : I don't want to use Actions class to implement mouse hover.

SAN QA

unread,
Mar 10, 2012, 10:04:41 AM3/10/12
to webdriver
HI Mathew

Mouse hover functions works so cool now for me ..

I had same issue earlier , but i resolved it using Actions objects
itself

If your website where you are testing if it public pping me the URL .
privately to my id i will solve the issue and give the code

Mattayi

unread,
Mar 10, 2012, 10:24:02 AM3/10/12
to webdriver
Please pass an example code for this url http://www.sothink.com/product/dhtmlmenu/store/phpdb/
php-level2.php

sai kiran

unread,
Mar 10, 2012, 11:49:00 AM3/10/12
to webd...@googlegroups.com
I didn't tried any mouseover or JS events to perform actions on your app. Just tried with click actions it worked..

driver.get("http://www.sothink.com/product/dhtmlmenu/store/phpdb/php-level2.php");
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
       
        driver.findElement(By.id("stUI3_txt")).click();
        driver.findElement(By.id("stUI5_txt")).click();
        driver.findElement(By.linkText("Support")).click();
        driver.findElement(By.linkText("Follow us on twitter")).click();
       
       
        /*driver.findElement(By.id("stUI10_txt")).click();
        driver.findElement(By.id("stUI12_cnt")).click();
        driver.findElement(By.linkText("Support")).click();
        */
        driver.quit();

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.


SAN QA

unread,
Mar 10, 2012, 1:12:12 PM3/10/12
to webdriver

HI
I have attached the code along with this.
I hope you are able to solve the issue

Below code is working code i have executed it it works fine for me
//launch the browser
           InternetExplorerDriver driver = new
InternetExplorerDriver();
           
 driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20));
             driver.Navigate().GoToUrl("http://www.sothink.com/product/
dhtmlmenu/store/phpdb/php-level2.php");
             IWebElement menu =
driver.FindElement(By.Id(("stUI3_txt")));
            Actions action = new Actions(driver);
            action.MoveToElement(menu).Perform();
            IWebElement submenu =
driver.FindElement(By.Id(("stUI5_cnt")));
            action.MoveToElement(submenu).Click().Perform();


If you find still you are facing issue let me know

--
Thank you
Santhosh Baby
Mob No +91-9611437246

Symphony Services | Software Automation Engineer

Think GREEN. Please consider the environment before printing this
email

"When The Winds of Change Blow..... Some people Build Walls & Others
Windmills.... Attitude Matters...."

Mattayi

unread,
Mar 12, 2012, 9:13:36 AM3/12/12
to webdriver
@SanQA, thanks man it worked :)
Will let you know if i am blocked on anything.

Vinay Kumar

unread,
Aug 9, 2012, 9:26:42 AM8/9/12
to webd...@googlegroups.com

Vinay Kumar

unread,
Aug 9, 2012, 9:28:15 AM8/9/12
to webd...@googlegroups.com
I have used same thing which u have posted. its not working for me.username,pwd,click on login button is performing but not able to hover the mouse.
Kindly,help me to resolve this

Vinay Kumar

unread,
Aug 10, 2012, 1:16:58 AM8/10/12
to webd...@googlegroups.com
Hi..

Did u get solution for the problem which i discussed with u??

David

unread,
Aug 10, 2012, 2:07:08 PM8/10/12
to webdriver
FYI, http://code.google.com/p/selenium/issues/detail?id=2067, comment
#60 & 64 give example of how to do mouse over (~hover) using
javascript.

Mukul Bansal

unread,
Mar 6, 2015, 2:40:41 PM3/6/15
to webd...@googlegroups.com

This will work with all versions of firefox
public void click(WebElement a,WebElement b)
{
try 
{
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover',true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
((JavascriptExecutor) driver).executeScript(mouseOverScript,a);
Thread.sleep(1000);
((JavascriptExecutor) driver).executeScript(mouseOverScript,b);
Thread.sleep(1000);
((JavascriptExecutor)driver).executeScript("arguments[0].click();",b);
} catch (Exception e) {
// TODO: handle exception
Reply all
Reply to author
Forward
0 new messages