Prasad Din
Prasad Din

Reputation: 65

Want to move down using scrollbar selenium webdriver

I need to move down using scrollbar on a web page using selenium webdriver

I used following code

Actions dragger = new Actions(driver);
WebElement draggablePartOfScrollbar = driver.findElement(By.xpath("/html/body/section[2]/div/div[2]/div/div/div"));
int numberOfPixelsToDragTheScrollbarDown = 5000;
dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();

still its not moving down...xpath is changing as per position of scrollbar...

Upvotes: 1

Views: 54103

Answers (6)

Dileep
Dileep

Reputation: 1

Actions dragger = new Actions(driver); WebElement draggablePartOfScrollbar = driver.findElement(By.xpath("/html/body/section[2]/div/div[2]/div/div/div")); int numberOfPixelsToDragTheScrollbarDown = 5000; dragger.moveToElement(draggablePartOfScrollbar).clickAndHold(draggablePartOfScrollbar).moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();

This code worked for me with following corrections.

  1. Having right xpath. I clicked on inspect element and right click on the element for xpath and copied.
  2. Add WebElement in clickandHold cast.
  3. Also make sure numberOfPixelsToDragTheScrollbarDown matches to the exact pixels you want to drag down.

It worked as charm. Thanks for the code.

Upvotes: 0

Marius Golban
Marius Golban

Reputation: 1

For Java the code is below:

public void moveOverElement(WebElement element)
        {
            Actions actions = new Actions(driver);

            actions.clickAndHold(element).moveByOffset(0,5000).release().perform();

        }

For definition WebElement, for the webelement you have to define only the path:

@FindBy (xpath = ".//*[contains(@class, 'link-name') and text() = 'QAEbox']")

    private WebElement createdQABoxElement;

Upvotes: 0

chandrasekar
chandrasekar

Reputation: 1

By

scroll=By.xpath("//*[@id='aspnetForm']/center/div/div[2]/table/tbody/tr[2]/td[1]/table/tbody");
        WebElement scrollUp = driver.findElement(scroll);
        scrollUp.sendKeys(Keys.PAGE_DOWN);
        scrollUp.sendKeys(Keys.PAGE_DOWN);
        scrollUp.sendKeys(Keys.PAGE_DOWN);
        scrollUp.sendKeys(Keys.PAGE_DOWN);

For scroll up:

scrollUp.sendKeys(Keys.PAGE_DOWN);

Upvotes: -1

SATYA
SATYA

Reputation: 52

  JavascriptExecutor js=(JavascriptExecutor)driver;
  js.executeScript("window.scrollBy(0,100)");

Upvotes: 0

Mahesh Reddy Atla
Mahesh Reddy Atla

Reputation: 529

My code is in python..hope it might help you and you can reproduce the same to java

actionChains = ActionChains(driver)
option=driver.find_element_by_class_name("mCSB_dragger_bar")
actionChains.click_and_hold(option).perform()
actionChains.move_by_offset(0,5000).perform()
actionChains.release()

The above code can be simplified as

actionChains = ActionChains(driver)
option=driver.find_element_by_class_name("mCSB_dragger_bar")
actionChains.click_and_hold(option).move_by_offset(0,5000).release().perform()

Upvotes: 1

Amith
Amith

Reputation: 7018

If you are trying to locate some element by scrolling down,the following code will scroll until the element is in view.

WebElement element = driver.findElement(By.id("id_of_element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(500); 

//do anything you want with the element

Upvotes: 4

Related Questions