在Selenium可以滑动?

时间:2012-08-25 14:27:06

标签: selenium webdriver selenium-webdriver

  

可能重复:
  How to move Horizontal Slider or Vertical Slider of jQuery using Selenium Webdriver

互联网中有许多滑块的例子,如

http://jqueryui.com/demos/slider/

是否可以使用Selenium移动滑块?

2 个答案:

答案 0 :(得分:10)

工作代码 -

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

答案 1 :(得分: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();

}