How to slide{move} slider in Selenium WebDriver

后端 未结 11 2032
花落未央
花落未央 2021-02-11 09:30

How to slide slider on horizontal progress bar URL: http://jqueryui.com/slider/ I tried to scroll the slider but it did not work for me. My code using Java is below:

         


        
相关标签:
11条回答
  • 2021-02-11 10:07

    Try the below code.

    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();
       }
    
    0 讨论(0)
  • 2021-02-11 10:09

    I think the slide is use css style format to scroll so we change element's style

    JavascriptExecutor js = (JavascriptExecutor) driver;
    
    WebElement a = driver.findElement(By.xpath("//div[@id='slider-1']/a"));
    
    js.executeScript("arguments[0].setAttribute('style', 'left: 30%;')",a);
    
    0 讨论(0)
  • 2021-02-11 10:17

    Try with this (Latest)

    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();
                
        }
    }
    
    0 讨论(0)
  • 2021-02-11 10:20

    Use this code

    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();
    
    0 讨论(0)
  • 2021-02-11 10:21

    Try 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);
            }
    
    }
    
    
    }
    
    0 讨论(0)
  • 2021-02-11 10:21

    I just tried another way to work on slider bar.

    My code:

        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();
        }
        }
            }
    
    0 讨论(0)
提交回复
热议问题