1

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...

6 Answers 6

4

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
Sign up to request clarification or add additional context in comments.

7 Comments

what is the element you are trying to find?
can you provide the html for this?
<div style="position: absolute; display: block;" class="mCSB_scrollTools"> <div class="mCSB_draggerContainer"><div oncontextmenu="return false;" style="position: absolute; height: 170px; top: 49px;" class="mCSB_dragger"> <div style="position: relative; line-height: 170px;" class="mCSB_dragger_bar"> </div></div><div class="mCSB_draggerRail"></div></div></div>
WebElement element = driver.findElement(By.id("id_of_element"));..here id of text area should be mentioned..have u done this?
This answer by Amith003 is basically the right idea. You can ALSO use JavaScriptExecutor to send Key.DOWN messages to do page downs by the keyboard. That is the way I do it because I couldn't get scrollIntoView to work either...
|
1

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()

Comments

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

Comments

0

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;

Comments

0

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.

Comments

-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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.