Java 硒 moveByOffset 不做任何事情

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22703159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:22:32  来源:igfitidea点击:

Selenium moveByOffset doesn't do anything

javaseleniumselenium-firefoxdriver

提问by diffeomorphism

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

我在 Linux Xubuntu 13.10 上使用 Firefox 28.0 运行最新的 selenium 2.41

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 moveByOffsetis not doing anything noticeable to the mouse, at all:

我试图让 FirefoxDriver 将鼠标移到页面上(在我的测试中,我使用了有线网页,它有很多悬停激活的菜单),但是moveByOffset鼠标没有做任何明显的事情,在全部:

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

我期待鼠标在不同元素上移动并触发所有悬停动作,但什么也没有发生

回答by A Paul

Please try using moveToElement. It should work.

请尝试使用 moveToElement。它应该工作。

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

回答by codefan-BK

The method moveByOffsetof class Actionsis or has been broken. See Selenium WebDriver Bug 3578

moveByOffset类的方法Actions被破坏或已经被破坏。请参阅Selenium WebDriver 错误 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.

一个项目成员 (barancev) 声称这个错误应该在 Selenium 2.42 版中得到修复。

Nevertheless I found the same error in version 2.44 running on openSUSE 12.3 with Firefox 33.0. moveToElementworks, moveToOffsetdoesn't.

尽管如此,我在使用 Firefox 33.0 的 openSUSE 12.3 上运行的 2.44 版中发现了相同的错误。moveToElement有效,moveToOffset没有。

回答by Frithjof Schaefer

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.

无论如何,这是有效的(Java)代码。请注意,我在没有参数的情况下调用了“release()”——无论是 dropable Element 还是 dragable Element 作为参数都对我有用。以及“moveToElement(dropable)”对我不起作用,这就是我手动计算偏移量的原因。

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

回答by Gaurav khurana

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.

我也在为此苦苦挣扎,对我有用的解决方案如下,我们必须将 1 添加到 X 或 Y 坐标。

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

看起来 (x,y) 将我们带到元素的边缘,在那里它不可点击

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

您甚至可以尝试将 +1 添加到 y 坐标也可以使用

   act.moveByOffset(NumberX+1, NumberY).click().build().perform(); 

回答by vishal

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

我建议,如果您的浏览器没有执行 movetoelement 并移动到偏移量,那么您为查找偏移量设置了错误的元素偏移量,您在 chrome 中使用 Cordinates 插件