Slide-bar and slide action using Webdriver

Some websites do have slider elements like you can see in some travel websites like given below. this moment is possible using mouse event and concept would be very much similar to the old article on mouse event. I will take a sample example of working code with dummy data inside so you can pass your input and see this working. Here is detailed explanation: You need to keep few things in mind:

  1. Slider bar size may or may not vary based on browser size.
  2. You may need to slide either by number of pixels or percentage of slider bar length.

I will take very generic example of to explain all steps. First reason to select this example is that the size of slider bar is adjusting based on browser size and secondly its available for everyone J. In the code below, I will first try to get the size of bar at the current browser size where I want to perform slider action and then based on req I will move the slider. Make sure you don’t change the browser size in between reading the slider bar size and doing sliding action.

  1. Open url via selenium code.

  2. Switch to the container frame to reach the slider and slider bar web element.

  3. Select the bar and get the width.

  4. Now we need to move slide to 50% (231px) or 100px. We can write small java/.net program to calculate desired percentage in pix using the width of the bar as 463 in my case. But I will move only 40pixels.
  5. Get web element for the slider on bar.

  6. Now we want move the slider in right side horizontally means towards Y axis. To perform movement action we will use action class and use mouse event. To move horizontally we need to move in Y direction of screen so we can use MoveByOffset

    i.e. X axis: 0 & Y axis: 40px. So first create action, Click and hold the slider, move to some pix as follows.

    Now script has completed and you can perform the whole action in one go!!!

Happy testing!!

Leave a Reply