6

I have a slider with min range is 0 and max range is 17; if I want to move the slider to specific position say, @10, how to do? I am using offset but it is not moving to specific position. Below is the code I have used; this is working fine but 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.ClickAndHold(slider);
        action.MoveByOffset(40, 0).Build().Perform();
  • Try to do it using for loop – Sachintha Feb 28 '18 at 6:26
2

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);
SliderAction.ClickAndHold(Slider)
    .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);
SliderAction.ClickAndHold(Slider)
    .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.

4

Try with below simple Examples:

Ex 1:

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

    Actions action= new Actions(driver);
    action.clickAndHold(Slider);
    action.moveByOffset(40,0);
    action.release().build();

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();
   act.build().perform();

Ex 3: Using For Loop

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

    Actions action= new Actions(driver);
    action.click(slider).build().perform();
    Thread.sleep(1000);
    for (int i = 0; i < 10; i++) 
    {
        action.sendKeys(Keys.ARROW_LEFT).build().perform();
        Thread.sleep(200);
    }


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

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.