Selenium: horizontal scroll using Actions class
Asked Answered
F

2

1

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

Fledgling answered 31/10, 2017 at 11:48 Comment(5)
What is WebElement slider = element("slider_div"); and WebElement scrollbar = element("scrollbar"); refering to exactly?Abstinence
Hi Debanjan, 'slider' refers to the scrolling div and 'scrollbar' refers to the scrollbar element. Let me paste the HTML below: <div class="lcs_customScroller column-8" data-scroll-control=".columnScroller"> <-----scrollbar <div class="lca_customScrollerOverflow" data-scroll-width=".columnScrollAuto" style="width: 16016px;"><------slider </div> </div>Fledgling
Can you provide more information related to Html page, you question is related to slide element or scroll page|elementEngland
My question is related to a scroll page|element . Its a custom scrollbar within a divFledgling
Can you share any sample page to try for element horizontal scroll. Is this your question right.England
E
2

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 «

  • Selenium java:

    WebElement element = driver.findElement(By.xpath("//div[@id='board']/div[1]/div[1]/div[2]") );
    for (int i = 0; i < 5; i++) {
        jse.executeScript("arguments[0].scrollTop += 200;", element);
    }
    
  • javascript | jquery:

    var objDiv = $x("//div[@id='board']/div[1]/div[1]/div[2]")[0];
    console.log( objDiv );
    
    objDiv.scrollTop += 100; 
    //objDiv.scrollTop = objDiv.scrollHeight;
    

Element Inner Horizontal Scroll «

  • Java Actions Class

    WebElement horizontalbar = driver.findElement(By.id("board") );
    Actions action = new Actions(driver);
    
    Actions moveToElement = action.moveToElement( horizontalbar );
    for (int i = 0; i < 5; i++) {
        moveToElement.sendKeys(Keys.RIGHT).build().perform();
    }
    

Scroll till the Element comes into view.

  • javascript

    public void scroll_Till_Element(String id) {
        WebElement element = driver.findElement(By.id( id ) );
        jse.executeScript("arguments[0].scrollIntoView(true);", element);
    }
    

Scroll till end of the page.

public void scrollPage() throws InterruptedException {
    driver.get("https://mcmap.net/q/206510/-selenium-scroll-till-end-of-the-page/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

England answered 31/10, 2017 at 14:52 Comment(0)
C
0

Use below Lines to scroll from left to right

((JavascriptExecutor) driver).executeScript("window.scrollBy(500000, 0)");

To scroll right to left use below lines:

((JavascriptExecutor) driver).executeScript("window.scrollBy(-500000, 0)");

This will scroll completely to the left or right as (50000) is a very big value to cover a page

Counterweigh answered 1/11, 2017 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.