9

I have a slider that uses html5 on an input e.g.

<input id="sliderWidget" title="Slide me" type="range" min="1" max="1.6" step="0.3" value="1.3">

I have been trying to use selenium to change the slider but traditional image slider controls are not working for me.... e.g.

Action dragAndDrop = builder.dragAndDropBy(sliderWidget,0,30).build();
dragAndDrop.perform();

Does anyone have any ideas how i can perform this incremental range change

thanks in advance

Walery Strauch
7,1308 gold badges56 silver badges61 bronze badges
asked Apr 30, 2013 at 17:22

3 Answers 3

15

You can select the element and use the send_keys method to send it the right/left arrow keys (which should increment/decrement the input). In Python:

from selenium.webdriver.common.keys import Keys
...
slider = page.find_element_by_id("sliderWidget")
for i in range(10):
 slider.send_keys(Keys.RIGHT)

That should increment the value by 10.

answered Sep 1, 2015 at 14:35

1 Comment

Not a bad idea - but I get WebDriverException: Message: unknown error: cannot focus element
2

You can set the value direct in JavaScript:

WebElement slider = webDriver.findElement(By.id("sliderWidget"));
System.out.println(slider.getAttribute("value"));
JavascriptExecutor js = (JavascriptExecutor) webDriver;
js.executeScript("javascript:document.getElementById(\"sliderWidget\").value=1.5;");
System.out.println(slider.getAttribute("value"));

Notice that sliderWidget was set to 1.5 but result was 1.6. This way you can test that step=0.3 is working well.

answered May 1, 2013 at 14:51

1 Comment

This is not a realistic test. This will work even if the element is inaccessible. And more importantly it does not trigger change handlers on the <input>.
-1

I've found the most stable and cross-browser way is to perform a binary search, dragging the mouse on the slider thumb until you hit the spot.

I have Python/webdriver code for this in my blog: http://blog.usetrace.com/?p=279

answered Jul 31, 2015 at 15:19

1 Comment

That's probably the most realistic way to do this. But it's pretty much a link-only answer. I certainly wouldn't be able to figure this out without following the link.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.