How to use selenium to change input value [type = 'range']

I have a slider that uses html5 on input for example.

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

I'm trying to use selenium to change the slider, but the traditional elements of the image slider do not work for me ... for example.

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

Does anyone have any ideas how I can accomplish this incremental range change?

early

+5
source share
3 answers

You can select an element and use the send_keys method to send it the left / right arrow keys (which should increase / decrease 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)

This should increase the value by 10.

+6
source

You can set the value directly 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"));

, sliderWidget 1,5, 1,6. , , step=0.3 .

+1

I found the most stable and cross-browser way - to perform a binary search by dragging the mouse onto the large slider of the slider until you hit the point.

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

-1
source

All Articles