Selenium moveByOffset doesn't do anything

杀马特。学长 韩版系。学妹 提交于 2019-12-01 00:44:53

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.

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

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

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

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

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