3

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

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

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

我不知道怎么做

4

5 回答 5

9

我计算出相当于Franz Ebner 的答案的Python 版本。以防万一它对某人有帮助

注意:在 Python 中,

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

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


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()
于 2012-11-23T11:49:50.947 回答
3

你试过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();

}
于 2012-06-21T13:31:34.997 回答
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();
于 2012-08-22T06:02:56.963 回答
2

生成动作链

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

 Actions builder = new Actions(driver); 
 Action dragAndDrop = builder.clickAndHold(someElement)
    .moveToElement(otherElement)
    .release(otherElement)
    .build(); 
 dragAndDrop.perform();
于 2012-06-21T14:29:42.643 回答
0

在这种情况下,我更喜欢使用以下代码移动滑块-

Actions builder = new Actions(driver);

Action dragAndDrop =

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

dragAndDrop.perform();

在这种特殊情况下,将滑块移动一个偏移量而不是使用 moveToElement(otherElement) 是有意义的。

希望这对您有所帮助。

于 2012-06-22T05:02:25.400 回答