seleniumselenium-webdriverscrollbaractionmcustomscrollbar

Selenium: horizontal scroll using Actions class


I have tried various to access this custom scroll bar on my web page through Selenium actions as well as Javascript Executor. The scroll bar scrolls through only 500 pixels when in fact I want it to scroll throughout the complete width. Any help is appreciated. Below is my current code snippet:

 WebElement slider = element("slider_div");
 WebElement scrollbar = element("scrollbar");
 int w = slider.getSize().getWidth();
 Actions move = new Actions(driver);
 move.dragAndDropBy(e, offset, 0).build() .perform();

I have also tried with the below solution, but still it did not work out:

WebElement slider = element("slider_div");
WebElement scrollbar = element("scrollbar");
int w = slider.getSize().getWidth();
clickByJavascript(slider);
Actions action = new Actions(driver); 
action.sendKeys(Keys.RIGHT).build().perform();`

Thanks in Advance!!!


Solution

  • I have tested your scenarion over the site specified below, with horizontal and vertical scrolls.

    Test URL « https://trello.com/b/LakLkQBW/jsfiddle-roadmap;

    Element Inner Vertical Scroll «

    Element Inner Horizontal Scroll «

    Scroll till the Element comes into view.

    Scroll till end of the page.

    public void scrollPage() throws InterruptedException {
        driver.get("https://stackoverflow.com/q/33094727/5081877");
    
        Actions actions = new Actions(driver);
        WebElement element = driver.findElement(By.xpath("//body") );
        Actions scrollDown = actions.moveToElement( element );
        scrollDown.keyDown(Keys.CONTROL).sendKeys(Keys.END).build().perform();
        //jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
    
        Thread.sleep( 1000 * 4 );
    
        /*Actions scrollUP = actions.moveToElement( element );
        scrollUP.keyDown(Keys.CONTROL).sendKeys(Keys.HOME).build().perform();*/
        jse.executeScript("window.scrollTo(0, -document.body.scrollHeight)");
    
        scroll_Till_Element( "answer-33142037" );
    }
    

    for javascript you can use any of these.

    window.scrollBy(0,800);
    window.scrollTo(0, -document.body.scrollHeight);
    scroll(0, -document.body.scrollHeight);
    

    @see