2012-08-25 19 views

回答

9

工作代碼 -

WebDriver driver = new InternetExplorerDriver(); 
driver.get("http://jqueryui.com/demos/slider/"); 
//Identify WebElement 
WebElement slider = driver.findElement(By.xpath("//div[@id='slider']/a")); 

//Using Action Class 
Actions move = new Actions(driver); 
Action action = move.dragAndDropBy(slider, 30, 0).build(); 
action.perform(); 

driver.quit(); 

來源 - https://gist.github.com/2497551

+0

這實際上是不正確的。這裏不需要「.build()」方法。上面的代碼 – djangofan

+0

不適用於我。 –

2

你有沒有試過在Action接口?

尤其是點 「生成動作鏈」 可以幫助你

/** 
* Moves a jQuery slider to percental position, don't care about directions 
* @param slider to move 
* @param percent to set the slider 
*/ 
public void moveSliderToPercent(WebElement slider, int percent){ 

    Actions builder = new Actions(this.driver); 

    Action dragAndDrop; 

    int height = slider.getSize().getHeight(); 
    int width = slider.getSize().getWidth(); 


    if(width>height){ 
     //highly likely a horizontal slider 
     dragAndDrop = builder.clickAndHold(slider).moveByOffset(-(width/2),0). 
         moveByOffset((int)((width/100)*percent),0). 
         release().build(); 
    }else{ 
     //highly likely a vertical slider 
     dragAndDrop = builder.clickAndHold(slider).moveByOffset(0, -(height/2)). 
         moveByOffset(0,(int)((height/100)*percent)). 
         release().build(); 
    } 


    dragAndDrop.perform(); 

} 
+0

上面的代碼對我來說工作不正常。我也試着用發佈的網站'http:// jqueryui.com/demos/slider /'。它不工作。 – Manigandan

+0

我沒有經常性的測試...但是它在我發佈時確實有效;) –