Jon87654
Jon87654

Reputation: 1

How to scroll down an element in Selenium java

I have a leader board element that I need to scroll down to view all players at the top of a web page using Selenium Java. Having spent the past 2 hrs googling I am only able to find answers by scrolling down a page that is not what I need.

I Have tried the following

        //click on leaderboard
    driver.findElement(By.xpath("//*[@id=\"root\"]/div/div/div[2]/div[1]/div[2]/div[1]/div/div")).click();

    //scroll
    WebElement scroll = driver.findElement(By.xpath("//*[@id=\"accordion-panel-1720\"]/div[2]/div"));
    Actions actions = new Actions(driver);
    actions.moveToElement(scroll).perform();

and

        WebElement scroll = driver.findElement(By.xpath("//*[@id=\"accordion-panel-1720\"]/div[2]/div"));
    Actions act = new Actions(scroll);
    act.sendKeys(Keys.PAGE_DOWN).build().perform(); //Page Down
    System.out.println("Scroll down perfomed");

Upvotes: 0

Views: 5129

Answers (2)

Santhosh
Santhosh

Reputation: 38

You can use Javascript executor

For scroll down,

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

For scroll Up,

js.executeScript("window.scrollBy(0,250)");

you can also use to scroll until element is visible

js.executeScript("arguments[0].scrollIntoView(true);", element);

You can refer to this site

Upvotes: 0

Jake White
Jake White

Reputation: 92

So far, I've found two ways to do this for me: using Keys.ARROW_DOWN, Keys.PAGE_DOWN or executing a Javascript to scroll the element up/down.

JavascriptExecutor jsExec = (JavascriptExecutor) driver;
jsExec.executeScript("document.getElementById('id').scrollTop += 100");

Upvotes: 1

Related Questions