Skip to main content

You are here

Scroll bars dragging

The following code demonstrates how to drag the vertical scroll bars on a window. The "Actions" class is used to click and hold on to the scroll bar and then it is dragged to a new location.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class ScrollBarDragging {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://baijs.nl/tinyscrollbar/");

Actions dragger = new Actions(driver);
WebElement draggablePartOfScrollbar = driver.findElement(By.xpath("//*[@id='scrollbar1']/div[1]/div/div"));

// drag downwards
int numberOfPixelsToDragTheScrollbarDown = 10;
for (int i=10;i<150;i=i+numberOfPixelsToDragTheScrollbarDown){
// this causes a gradual drag of the scroll bar, 10 units at a time
dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
Thread.sleep(1000L);
}

// now drag opposite way (downwards)
numberOfPixelsToDragTheScrollbarDown = -10;
for (int i=150;i>10;i=i+numberOfPixelsToDragTheScrollbarDown){
// this causes a gradual drag of the scroll bar, -10 units at a time
dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
Thread.sleep(1000L);
}

driver.quit();
}
}

Here is the c# + NUnit version of the same code in Visual Studio. Note you have to nfigure both NUnit + Selenium in Visual Studio in order to run the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;


/*
This test requires NUnit + Selenium reference
*/
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Firefox;
/*--------------------------------------------------------------*/

{
    [TestFixture]
    class ScrollBarDraggingExample
    {
        [Test]
        public void ScrollBarDragging() {

            IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://baijs.nl/tinyscrollbar/");

            Actions dragger = new Actions(driver);
            IWebElement draggablePartOfScrollbar = driver.FindElement(By.XPath("//*[@id='scrollbar1']/div[1]/div/div"));

            // drag downwards
            int numberOfPixelsToDragTheScrollbarDown = 10;
            for (int i = 10; i < 150; i = i + numberOfPixelsToDragTheScrollbarDown)
            {
                // this causes a gradual drag of the scroll bar, 10 units at a time
                dragger.MoveToElement(draggablePartOfScrollbar).ClickAndHold().MoveByOffset(0, numberOfPixelsToDragTheScrollbarDown).Release().Perform();
                Thread.Sleep(100);
            }

            // now drag opposite way (downwards)
            numberOfPixelsToDragTheScrollbarDown = -10;
            for (int i = 150; i > 10; i = i + numberOfPixelsToDragTheScrollbarDown)
            {
                // this causes a gradual drag of the scroll bar, -10 units at a time
                dragger.MoveToElement(draggablePartOfScrollbar).ClickAndHold().MoveByOffset(0, numberOfPixelsToDragTheScrollbarDown).Release().Perform();
                Thread.Sleep(100);
            }

            driver.Quit();
        }
    }
}

Category: 

Language: 

0
No votes yet

Add new comment

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.

Disclaimer

Note that the codes and ideas expressed on this site are the authors personal views. The content of this website are the personal notes based on the learning experience of the author. The author will not be responsible for any error or damages resulting from you using the content of this website.

Premium Drupal Themes by Adaptivethemes