简体   繁体   English

使用Java和Firefox在Selenium Webdriver中移动滑块

[英]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. 我在使用Selenium Webdriver(版本2.32.0)和Firefox(版本21.0)时遇到问题,试图更改滑块上的值。

I wrote a Java code like this: 我写了这样的Java代码:

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. 我尝试了在Web上找到的每个代码,每次更改,但仍然无法正常工作。 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 我终于使它与jQuery滑块演示一起工作

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 . 但是,对于使用Xpath //div[@id='slider']/a jQuery UI Slider演示页,它仍然不起作用。 What is the problem? 问题是什么?

This code works absolutely fine for me. 这段代码对我来说绝对不错。 program handles slider of website : Homeshope18.com Check it out: 程序处理网站的滑块: Homeshope18.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();

Using Actions class, firs use clickAndHold("WebElemnt"); 使用Actions类, clickAndHold("WebElemnt");使用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 然后要水平移动,我们需要在屏幕的Y方向上移动,以便可以使用movebyoffset ,即X轴:0和Y轴:40像素

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 要垂直移动,我们需要在屏幕的X方向上移动,以便可以使用movebyoffset ,即X轴:40px和Y轴:0

The sample code would be : 示例代码为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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