如何使用Selenium Webdriver移动jQuery的Horizo​​ntal Slider或Vertical Slider

时间:2012-06-21 12:44:12

标签: jquery selenium slider webdriver

我想制作selenium脚本移动以下网站上给出的滑块

示例名称是如何更改jQuery UI Slider的方向

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

我不知道如何做到这一点

5 个答案:

答案 0 :(得分:6)

我研究了相当于Franz Ebner's answer的Python。以防万一它可以帮助某人

注意: 在Python中,

  1. find_element_by_XXX在框架中找不到元素,除非 你使用switch_to_frame(不确定其他语言)

  2. 负( - )偏移值无法按预期工作,因此仅根据传递给方法的百分比计算的偏移值移动


  3. 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()
    

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

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

}

答案 3 :(得分:2)

生成行动链

Actions链接生成器实现Builder模式以创建包含一组其他操作的CompositeAction。这应该通过配置Actions链生成器实例并调用它的build()方法来获得复杂的操作来简化构建操作:

 Actions builder = new Actions(driver); 
 Action dragAndDrop = builder.clickAndHold(someElement)
    .moveToElement(otherElement)
    .release(otherElement)
    .build(); 
 dragAndDrop.perform();

答案 4 :(得分:0)

我希望在这种情况下使用以下代码移动滑块 -

Actions builder = new Actions(driver);

Action dragAndDrop =

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

dragAndDrop.perform();

在这种特殊情况下,通过偏移移动滑块而不是使用moveToElement(otherElement)是有道理的。

希望这会对你有所帮助。