如何在Selenium WebDriver中滑动{move}滑块

时间:2013-03-02 06:56:38

标签: java selenium selenium-webdriver slider

如何在水平进度条上滑动滑块 网址:http://jqueryui.com/slider/ 我试图滚动滑块,但它对我不起作用。我使用Java的代码如下:

 public class TestClass 
 {
    public static void main(String[] args) {         
    File file = new File("D:\\Driver\\IEDriverServer.exe");
    System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
    WebDriver driver = new InternetExplorerDriver();
    driver.get("http://jqueryui.com/slider/");

    WebElement slider = driver.findElement(By.xpath("//div[@id='slider']/a"));
    Actions move = new Actions(driver);
    Action action = (Action) move.dragAndDropBy(slider, 30, 0).build();
    ((Actions) action).perform();

}


}

控制台

   Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to    find element with css selector == html body div#slider.ui-slider a.ui-slider-handle   (WARNING: The server did not provide any stacktrace information)
  Command duration or timeout: 286 milliseconds
  For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
 Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 21:09:54'
 System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version:   '1.7.0_13'
 Driver info: driver.version: RemoteWebDriver
 Session ID: 79edc751-a9a3-4ecb-a171-d2b43c088a66
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:188)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:498)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:268)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:345)
at org.openqa.selenium.By$ByCssSelector.findElement(By.java:406)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:260)
at testpakage.testclass.main(testclass.java:30)

11 个答案:

答案 0 :(得分:9)

请尝试以下代码。

public class TestClass 
   {
        public static void main(String[] args) {         
        File file = new File("D:\\Driver\\IEDriverServer.exe");
        System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
        WebDriver driver = new InternetExplorerDriver();
        driver.get("http://jqueryui.com/slider/");

        WebDriver driver = new InternetExplorerDriver();
        driver.get("http://jqueryui.com/slider/");
        driver.switchTo().frame(0); //need to switch to this frame before clicking the slider
        WebElement slider = driver.findElement(By.xpath("//div[@id='slider']/span"));
        Actions move = new Actions(driver);
        Action action = (Action) move.dragAndDropBy(slider, 30, 0).build();
        action.perform();
   }

答案 1 :(得分:6)

试试sendkeys!

public class TestClass 
 {
    public static void main(String[] args) {         
    File file = new File("D:\\Driver\\IEDriverServer.exe");
    System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
    WebDriver driver = new InternetExplorerDriver();
    driver.get("http://jqueryui.com/slider/");

    WebElement slider = driver.findElement(By.xpath("//div[@id='slider']/a"));

    for (int i = 1; i <= "how many times you want" ; i++) {
            slider.sendKeys(Keys.ARROW_RIGHT);
        }

}


}

答案 2 :(得分:1)

我只是尝试了另一种方法来处理滑块。

我的代码:

    public void sliderCode(int iSliderAdjustValue){
    File file = new File("D:\\Driver\\IEDriverServer.exe");
    System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
    WebDriver driver = new InternetExplorerDriver();
    driver.get("http://jqueryui.com/slider/");

    WebDriver driver = new InternetExplorerDriver();
    driver.get("http://jqueryui.com/slider/");
    driver.switchTo().frame(0); //need to switch to this frame before clicking the slider
    WebElement slider = driver.findElement(By.xpath("//div[@id='slider']/a"));
    Actions builder= new Actions(driver);
    int iCount = 0;
    if (iRange > 0) {
    for(iCount = 0; iCount < iRange; iCount++) {
    builder.moveToElement(slider).click(slider).sendKeys(Keys.ARROW_UP).perform();
              }
    } else {
    for (iCount = 0; iCount > iRange; iCount--) {
    builder.click(slider).sendKeys(Keys.ARROW_DOWN) .perform();
    }
    }
        }

答案 3 :(得分:0)

这段代码对我来说绝对合适 它处理网站的Slider:homeshop18.com 试试这段代码:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.homeshop18.com/fashion-jewellery/category:15143/filter_Theme:%28%22Traditional+Wear%22+%22Cuff+%26+Kada%22+%22Daily+Wear%22+%22Maang+Tikka%22+%22Openable+Round%22+%22Round%22+%22Openable+Oval%22%29/sort:Popularity/inStock:true/?it_category=HP&it_action=JW-HPSP01&it_label=HP-HPSP01-131021235900-PD-JW-ZC-VK-SC_DiwaliFestWeddingJewellery&it_value=0");

WebElement Slider = driver.findElement(By.xpath("//*[@id='slider-range']/a[1]"));
Thread.sleep(3000);

Actions moveSlider = new Actions(driver);
Action action = moveSlider.dragAndDropBy(Slider, 30, 0).build();

action.perform();

答案 4 :(得分:0)

使用此代码

Actions dragger = new Actions(driver);
WebElement draggablePartOfScrollbar = driver.findElement(By.xpath("**xpath of slider**"));
int numberOfPixelsToDragTheScrollbarDown = 5000;
dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();

答案 5 :(得分:0)

使用CSS会很好

public class testclass 

{

  public static void main(String[] args)

  {         


   File file = new File("D:\\Driver\\IEDriverServer.exe");

    System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
    WebDriver driver = new InternetExplorerDriver();
    driver.get("http://jqueryui.com/slider/");

    WebElement slider = driver.findElement(By.cssSelector("css=div#slider > a"));
    Actions move = new Actions(driver);
    Action action = (Action) move.dragAndDropBy(slider, 30, 0).build();
    ((Actions) action).perform();

}

}

答案 6 :(得分:0)

我认为幻灯片使用css样式格式滚动 所以我们改变了元素的风格

JavascriptExecutor js = (JavascriptExecutor) driver;

WebElement a = driver.findElement(By.xpath("//div[@id='slider-1']/a"));

js.executeScript("arguments[0].setAttribute('style', 'left: 30%;')",a);

答案 7 :(得分:0)

//Try this code
WebElement e = driver.findElement(By.className("Class name of Dragger"));
Actions move = new Actions(driver);
move.moveToElement(e).clickAndHold().moveByOffset(0,250).release().perform();
Thread.sleep(3500);

答案 8 :(得分:0)

    package UKRTEST;

    import java.util.concurrent.TimeUnit;

    import org.openqa.selenium.By;
    import org.openqa.selenium.Dimension;
    import org.openqa.selenium.WebDriver;

    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;

    import org.openqa.selenium.interactions.Actions;

    public class Ex_DragndDrop {

    public static void main(String[] args) throws InterruptedException {

    WebDriver driver;
    System.setProperty("webdriver.chrome.driver", "E:\\Chrome Driver\\chromedriver.exe");
    driver = new ChromeDriver();

    String URL = "http://www.seleniumeasy.com/test/drag-drop-range-sliders-demo.html";

    driver.get(URL);

    driver.manage().window().maximize();

    driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);

    Thread.sleep(3000);
    Actions builder = new Actions(driver);

    WebElement draggablePartOfScrollbar = driver
            .findElement(By.xpath("/html/body/div[2]/div/div[2]/section/div[1]/div[1]/div/input"));
    // To get pixel count of Slider X axis and Y axis(267,25)

    Dimension sliderWidth = draggablePartOfScrollbar.getSize();
    System.out.println(sliderWidth);

    // We need to give below value in +00 to move slider to 50 and +130 to move slider to 100,-100 to move slider to 12
    int numberOfRangeToDragTheSlider = -100;
    builder.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(numberOfRangeToDragTheSlider, 0)
            .release().perform();

}

}

答案 9 :(得分:0)

使用以下方法在播放器上进行正向和反向搜索

公共无效的dragByPixelElement(WebElement元素,int x,int y){

    Actions act = new Actions(driver);
    act.dragAndDropBy(element, x, y).perform();
    Reporter.log("Drag and Drop action is performed by Pixel at " + x + "to" + y + " Position");

}

对于正向搜索,请使用正x值;对于反向搜索,请使用负值,如我在此代码中所使用的:

答案 10 :(得分:0)

尝试一下(最新)

public class TestSliders {
    
    public static WebDriver driver;

    public static void main(String[] args) {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.get("https://jqueryui.com/slider/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.switchTo().frame(0); 
        WebElement slider = driver.findElement(By.xpath("//*[@id=\"slider\"]/span"));
        new Actions(driver).dragAndDropBy(slider, 40, 0).perform();
            
    }
}