Selenium moveByOffset doesn't do anything

梦想与她 提交于 2019-11-30 18:11:36

问题


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


回答1:


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.




回答2:


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();
}



回答3:


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(); 



回答4:


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();



回答5:


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



来源:https://stackoverflow.com/questions/22703159/selenium-movebyoffset-doesnt-do-anything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!