2014-03-28 154 views
3

我在Linux上运行的Xubuntu最新硒2.41与Firefox 28.0 13.10硒moveByOffset没有做任何事情

我试图让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(); 
    } 
} 

我期待鼠标放在不同的元素移动并触发所有的悬停动作,但没有任何事情发生

回答

-1

请尝试使用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(); 
+0

但我不想移动到特定的元素,我想在给定的方向上自由移动。如果我站在画布上怎么办?我想在任何dx,dy移动,不管我站在哪里 – diffeomorphism

+1

你能尝试类似val action =(new Actions(driver))。dragAndDropToOffset(el,X,Y).perform() –

2

Actions类的方法moveByOffset是或已被打破。请参阅Selenium WebDriver Bug 3578

(该错误在本bug文档中有更多描述)。

一个项目成员(barancev)声称这个错误应该已经用Selenium版本2.42修复了。

不过,我发现运行在openSUSE 12.3上的版本2.44与Firefox 33.0相同的错误。 moveToElement作品,moveToOffset不。

+0

这里。 moveToElement有效,moveToOffset不适用于2.44。 – fuiiii

1

我挣扎以及拖放工作。

如果dragtarget不可见,硒似乎有问题,因此需要滚动。

无论如何,这是(Java)代码的作品。请注意,我在不带参数的情况下调用“release()” - 无论是可丢弃元素还是可拖动元素作为参数都适用于我。以及“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(); 
} 
0

我也在为此付出努力,而对我而言的解决方案是,我们必须在X或Y坐标上加1。

看起来像(X,Y)把我们带到了元件的边缘,其中它的不可点击

我下面的工作

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

你甚至可以尝试添加+1至y坐标也作品

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