2014-03-28 171 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();