1

I am trying to do a sample program with selenium webdriver. I am using libraries from Selenium-java-2.53.1.

Here is my sample program

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

public class ScrollWebPage {

    WebDriver driver;
    String URL="https://www.gmail.com";

    @BeforeClass
    public void setUp(){
        driver =  new FirefoxDriver();
        driver.get(URL);
        driver.manage().window().maximize();
    }

    @Test(priority=1)
    public void scrollingToBottom(){
        ((JavascriptExecutor) driver).executeScript(URL, "window.scrollTo(0,document.body.scrollHeight)");  
    }

    @AfterClass
    public void tearDown(){
        driver.quit();
    }
}

The page is getting opened but it is not able to scroll down. seems an issue with executeScript()

Please help

2 Answers 2

1

.executeScript() expecting JavaScript string expression as first arguments while you are providing simply a String as Url which is not an JavaScript expression as exception says, You need to change :-

((JavascriptExecutor) driver).executeScript(URL, "window.scrollTo(0,document.body.scrollHeight)"); 

to

((JavascriptExecutor) driver).executeScript("window.scrollTo(0,document.body.scrollHeight)"); 

Note :- .executeScript() expect arguments like String arg0, Object... arg1 which means first arguments should be String but it should be JavaScript expression and second arguments should be Array of Object like Object[]

In your case no need to provide URL as arguments if you simply want to execute scrolling function.

Hope it will help you..:)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I removed URL and it worked. Thanks for making me understand what it means.
0

Simply Use as below to see the scroll working. Try in some other page because gmail don't have a much bigger page to feel the scroll.

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

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.