简体   繁体   中英

Move slider in Selenium Webdriver with Java and Firefox

I have a problem using Selenium Webdriver (version 2.32.0) and Firefox (21.0), trying to change the values on a slider.

I wrote a Java code like this:

private void selectGiftCardPrice() throws TestingException {
        try {
            WebElement slider = getDriver().findElement(
                    By.cssSelector("div.sliderHandle"));
            Actions move = new Actions(getDriver());
            move.dragAndDropBy(slider, 90, 0);
            move.build().perform();
            sleep(4000);
        } catch (Exception e) {
            log.info(e);
            throw new TestingException("e");
        }

I tried out every code I found on the Web, every change, and it still does not work. It does not show any problem, just finds the element, and does nothing. Any idea what it is, or what can I do?

EDIT from comment:

I finally made it working with jQuery slider demo

driver.get("http://jqueryui.com/resources/demos/slider/multiple-vertical.html");
WebElement slider = driver.findElement(By.xpath("//div[1]/a[contains(@class,'ui-slider-handle')]"));‌

But it is still not working for me with jQuery UI Slider demo page using Xpath //div[@id='slider']/a . What is the problem?

This code works absolutely fine for me. program handles slider of website : Homeshope18.com Check it out:

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();

Using Actions class, firs use clickAndHold("WebElemnt");

Then to move horizontally we need to move in the Y direction of the screen so we can use movebyoffset , ie X-axis: 0 & Y axis: 40px

To move vertically we need to move in the X direction of the screen so we can use movebyoffset , ie X-axis: 40px & Y axis: 0

The sample code would be :

Actions slider=new Actions(driver);
slider.clickAndHold("WebElemnt");
slider.movebyoffset(0,40).build.perform();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM