4

I'm running latest selenium 2.41 with Firefox 28.0 on Linux Xubuntu 13.10

I'm trying to get FirefoxDriver to move the mouse over the page (in my test, I've used the wired webpage, that has a lot of hover-activated menus), but the moveByOffset is not doing anything noticeable to the mouse, at all:

package org.openqa.mytest;

import java.util.List;
import java.io.File;
import java.lang.*;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.interactions.*;

import org.apache.commons.io.FileUtils;

public class Example {
    public static void main(String[] args) throws Exception {
        // The Firefox driver supports javascript 
    FirefoxProfile profile = new FirefoxProfile();
    profile.setEnableNativeEvents(true);
        WebDriver driver = new FirefoxDriver(profile);

        // Go to the Google Suggest home page
        driver.get("http://www.wired.com");

    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        // now save the screenshto to a file some place
        FileUtils.copyFile(scrFile, new File("./screenshot.png"));


    Actions builder = new Actions(driver);
    Action moveM = builder.moveByOffset(40, 40).build();
    moveM.perform();

    Action click = builder.click().build();
    click.perform();
    //click.release();

    Action moveM2 = builder.moveByOffset(50, 50).build();
    moveM2.perform();

    Action click2 = builder.click().build();
    click2.perform();
    //click2.release();

    Action moveM3 = builder.moveByOffset(150, 540).build();
    moveM3.perform();

    for( int i=0; i < 1000; i++)
    {
        moveM = builder.moveByOffset(200, 200).build();
        moveM.perform();
        Thread.sleep(500);
        moveM = builder.moveByOffset(-200, -200).build();
        moveM.perform();
        Thread.sleep(500);
    }
    //Action click3 = builder.click().build();
    //click3.perform();
    //click3.release();

    scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        // now save the screenshto to a file some place
        FileUtils.copyFile(scrFile, new File("./screenshot2.png"));

        driver.quit();
    }
}

I'm expecting the mouse the move over the different elements and trigger all the hover actions, but nothing is happening

diffeomorphism
  • 991
  • 2
  • 10
  • 27

5 Answers5

2

The method moveByOffset of class Actions is or has been broken. See Selenium WebDriver Bug 3578

(The error is described some lines more down in this bug document).

A project member (barancev) claims that this error should have been fixed with Selenium version 2.42.

Nevertheless I found the same error in version 2.44 running on openSUSE 12.3 with Firefox 33.0. moveToElement works, moveToOffset doesn't.

codefan-BK
  • 310
  • 2
  • 9
1

I struggled as well getting drag and drop working.

It seems as if selenium has problems if the dragtarget is not visible, thus scrolling is requiered.

Anyway, that's the (Java) code that works. Note that I call "release()" without an argument - neither the dropable Element nor the dragable Element as argument worked for me. As well as "moveToElement(dropable)" didnt work for me, that's why I calculated the offset manually.

public void dragAndDrop(WebElement dragable, WebElement dropable,
        int dropableOffsetX, int dropableOffsetY) {
    Actions builder = new Actions(driver);

    int offsetX = dropable.getLocation().x + dropableOffsetX
            - dragable.getLocation().x;
    int offsetY = dropable.getLocation().y + dropableOffsetY
            - dragable.getLocation().y;

    builder.clickAndHold(dragable).moveByOffset(offsetX, offsetY).release()
            .perform();
}
Frithjof Schaefer
  • 1,135
  • 11
  • 22
0

i was also struggling with this and the solution that worked for me is below we have to add 1 to either X or Y co-ordinate.

Looks like (x,y) takes us to the edge of the element where its not clickable

Below worked for me

    WebElement elm = drv.findElement(By.name(str));

    Point pt = elm.getLocation();

    int NumberX=pt.getX();
    int NumberY=pt.getY();

    Actions act= new Actions(drv);
    act.moveByOffset(NumberX+1, NumberY).click().build().perform();

you could even try adding +1 to y coordinate that also works

   act.moveByOffset(NumberX+1, NumberY).click().build().perform(); 
Gaurav Khurana
  • 3,423
  • 2
  • 29
  • 38
-1

Please try using moveToElement. It should work.

Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath("<XPATH HERE>"));
action.moveToElement(we).moveToElement(webdriver.findElement(By.xpath("/expression-here"))).click().build().perform();
A Paul
  • 8,113
  • 3
  • 31
  • 61
  • But I don't want to move to particular elements, I want to move freely in a given direction. What if I'm standing on a canvas? I want to move in any dx,dy regardless of where I'm standing on the page – diffeomorphism Mar 28 '14 at 16:39
  • 1
    can you try something like val action = (new Actions(driver)).dragAndDropToOffset(el, X, Y).perform() – A Paul Mar 29 '14 at 19:27
-1

i suggest that if your browser is not perform movetoelement and move to offset then you have put wrong offset of element for find offset you use Cordinates plugin in chrome

vishal
  • 1