arushi3112
arushi3112

Reputation: 21

How to press Page Down key in Selenium to scroll down a webpage through Java?

I want to scroll down my web page. What Java command should I use in Selenium?

Upvotes: 1

Views: 16390

Answers (3)

Narayanan.pk
Narayanan.pk

Reputation: 1

Please refer the code below :

1) Using Action class****

WebDriver driver = new ChromeDriver();
            //Creating an object 'action' 
Actions action = new Actions(driver);
            //open SoftwareTestingMaterial.com
driver.get(Site URL);   // Give site URL as name of the web page
            //sleep for 3secs to load the page
Thread.sleep(3000);
            //SCROLL DOWN
action.sendKeys(Keys.PAGE_DOWN).build().perform();
Thread.sleep(3000);
            //SCROLL UP
action.sendKeys(Keys.PAGE_UP).build().perform();

Upvotes: 0

Farheen Khanum
Farheen Khanum

Reputation: 50

Use the below code and try,

        JavascriptExecutor js = (JavascriptExecutor) driver;

        // Launch the application       
        driver.get("http://demo.guru99.com/test/guru99home/");

        //This will scroll the web page till end.       
        js.executeScript("window.scrollTo(0, document.body.scrollHeight)");

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193298

Scrolling down a web page is not a valid usecase which can be validated perhaps you want to scroll down to bring a WebElement within the Viewport to interact with it. To achieve that you can use the executeScript() method as follows :

((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", element);

Upvotes: 1

Related Questions