I have a slider whose minimum range is 0 and maximum range is 17; if I want to move the slider to specific position say, @10, how can I do it?

I am using offset, but it is not moving to a specific position. Below is the code I have used; this is working fine, but it is not moving the slider to the desired expected position.

IWebElement slider = driver.FindElement(By.XPath(".//*[@id='mcl-range__nutrition.dietaryFiber']"));
int slidersizewidth = slider.Size.Width;//640
Actions action = new Actions(driver);
action.MoveByOffset(40, 0).Build().Perform();
  • Try to do it using for loop
    – Sachintha
    Feb 28, 2018 at 6:26

3 Answers 3


I see there's already a good answer for this but I'd just like to add my explicit solution in case it helps anyone.

This solution allows you to explicitly say what amount you want any slider to be instead of passing in pixel values or manually calculating it for each slider.

So quite simply to move the slider we need to call something like.

IWebElement Slider = driver.FindElement(By.Xpath("//*[@id='abc']"))
Actions SliderAction = new Actions(driver);
    .MoveByOffset((-(int)Slider.Size.Width / 2), 0)
    .MoveByOffset(Pixels, 0).Release().Perform();

The problem with this is here we tell the driver how many pixels to move the slider instead of the actual amount that we want to move it to.

So in order to convert the amount we want to set the slider to, to the pixels this would require we can use the following equation expressed through code.

public static int GetPixelsToMove(IWebElement Slider, decimal Amount, decimal SliderMax, decimal SliderMin)
        int pixels = 0;
        decimal tempPixels = Slider.Size.Width;
        tempPixels = tempPixels / (SliderMax - SliderMin);
        tempPixels = tempPixels * (Amount - SliderMin);
        pixels = Convert.ToInt32(tempPixels);
        return pixels;
  • Where SliderMax and SliderMin are the maximum and minimum amounts the slider can be set to
  • Amount is the amount value we want to set the slider to
  • Width is how wide the slider is in pixels
  • And Pixels is the calculated output value in pixels which is how far we will have to move the slider

For example:

If I had a slider to represent a range of days which was 100 pixels wide, had a minimum value of 2 days, a maximum value of 18 days, and we wanted to set it to be 7 days, we'd use a method like this.

IWebElement Slider = driver.FindElement(By.Xpath("//*[@id='abc']"))
int PixelsToMove = GetPixelsToMove(Slider, 7, 18, 2)
Actions SliderAction = new Actions(driver);
    .MoveByOffset((-(int)Slider.Size.Width / 2),0)
    .MoveByOffset(PixelsToMove, 0).Release().Perform();

And this would set the slider to be 7 days.

Full disclosure, this was taken from my repo here where it's already packaged as a class library for C# if you want to save some time with some additional methods to help automation.


Try with below simple Examples:

Ex 1:

    WebElement slider = driver.FindElement(By.XPath("//*[@id='mcl-range__nutrition.dietaryFiber']")); 

    Actions action= new Actions(driver);

Ex 2:

   int x=10;
   WebElement slider = driver.FindElement(By.XPath("//*[@id='mcl-range__nutrition.dietaryFiber']"));
   int width = slider.getSize().getWidth();
   Actions act= new Actions(driver);
   act.moveToElement(slider, ((width*x)/100), 0).click();

Ex 3: Using For Loop

    WebElement slider = driver.FindElement(By.XPath("//*[@id='mcl-range__nutrition.dietaryFiber']")); 

    Actions action= new Actions(driver);
    for (int i = 0; i < 10; i++) 

NOTE: Change arrow RIGHT/LEFT/UP/DOWN as per your condition. 

I found that calculating the offset by pixels was off due to rounding from float to int, so the solution I found was to first move the slider to the beginning and then move the arrow key in a loop. Each arrow key move to the right is one unit of the slider.

Actions action = new Actions(_driver);

var width = slider.Size.Width;
//the moment you issue a click Selenium clicks in the center of the slider
//therefore to move slider to beginning we need to move left half of the slider's length
action.ClickAndHold(slider).MoveByOffset(-(int)(width / 2), 0).Perform();
int increment = 50;
for (int i = 0; i < increment ; i++)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.