2012-06-21 128 views

回答

2

生成動作鏈

的操作鏈發生器實現了Builder模式創建一個包含組的其他行爲的CompositeAction。這應該通過配置Actions鏈生成器實例並調用它的build()方法來獲取複雜動作來簡化構建操作:

Actions builder = new Actions(driver);

行動dragAndDrop = builder.clickAndHold(someElement) .moveToElement(otherElement) .release(otherElement) .build();

dragAndDrop.perform();

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){ 
     //high likely a horizontal slider 
     dragAndDrop = builder.clickAndHold(slider).moveByOffset(-(width/2),0). 
         moveByOffset((int)((width/100)*percent),0). 
         release().build(); 
    }else{ 
     //high likely a vertical slider 
     dragAndDrop = builder.clickAndHold(slider).moveByOffset(0, -(height/2)). 
         moveByOffset(0,(int)((height/100)*percent)). 
         release().build(); 
    } 


    dragAndDrop.perform(); 

} 
0

我寧願通過使用以這種情勢

Actions builder = new Actions(driver); 

Action dragAndDrop = 

builder.clickAndHold(someElement).moveByOffset(xOffset,yOffset).release().build(); 

dragAndDrop.perform(); 

下面的代碼移動滑塊它使某種意義上通過移動滑塊的偏移,而不是在此特定情況下,使用moveToElement(otherElement) 。

希望這可以幫助你。

+0

在JQuery滑塊的情況下,這隻會將滑塊移出中間......因爲你開始移動的點是WebElement的中心 –

3

工作代碼 -

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

我計算出的Python相當於Franz Ebner's answer。以防萬一,如果它可以幫助別人

注: 在Python,

  1. find_element_by_XXX不框架內找到一個元素,除非 使用switch_to_frame(不知道其他語言)

  2. 負( - )的偏移量如預期的,因此僅由計算出的偏移值基於百分比傳遞到方法

移動值不工作3210
def check(self, percent): 
    driver = self.driver 
    driver.get("http://jqueryui.com/demos/slider/"); 
    driver.switch_to_frame(0) 
    driver.switch_to_active_element() 

    slidebar = driver.find_element_by_id("slider") 
    height = slidebar.size['height'] 
    width = slidebar.size['width'] 

    move = ActionChains(driver); 
    slider = driver.find_element_by_xpath("//div[@id='slider']/a") 

    if width > height: 
     //highly likely a horizontal slider 
     move.click_and_hold(slider).move_by_offset(percent * width/100, 0).release().perform() 
    else: 
     //highly likely a vertical slider 
     move.click_and_hold(slider).move_by_offset(percent * height/100, 0).release().perform() 

    driver.switch_to_default_content() 
+0

聽起來像[mine](http://stackoverflow.com/a/11139314/1300626):) –

+0

垂直滑塊上的move_by_offset應該在y軸上移動。所以move_by_offset(0,percent * width/100) – Derorrist

相關問題