Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
2.1k views
by The go-to Tester (218 points)

I am using Javascript Executor to scroll vertically in a web page. I am unable to do it because there is an error.

My code is as follows-: 

System.setProperty("webdriver.gecko.driver", "C://Selenium driver/geckodriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver();
driver.get("https://www.bbc.com");
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("windows.scroll(0,500)");
driver.close();
 
The error that I amk getting is-: 
Exception in thread "main" org.openqa.selenium.JavascriptException: ReferenceError: windows is not defined
Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'
System info: host: 'HOME-PC', ip: '192.168.0.10', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{rotatable=false, raisesAccessibilityExceptions=false, marionette=true, firefoxOptions={args=[], prefs={}}, appBuildId=20161208153507, moz:firefoxOptions={args=[], prefs={}}, version=, platform=XP, proxy={}, command_id=1, specificationLevel=0, acceptSslCerts=false, processId=2384, browserVersion=50.1.0, platformVersion=6.1, XULappId={ec8030f7-c20a-464f-9b0e-13a3a9e97384}, browserName=firefox, takesScreenshot=true, takesElementScreenshot=true, platformName=windows_nt}]
Session ID: 9931ef9e-11ff-458b-a721-a6a614f5cf26
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:127)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:93)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:42)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:537)
at myPackage.JSExecutor.main(JSExecutor.java:17)

 

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)
selected by
 
Best answer

There is a typing mistake in your Javascript. It should be window.scroll. You are using windows.scroll.

You can even try scrolling up should be as below.

((JavascriptExecutor) driver).executeScript("scroll(0,-80);");
 
To scroll down
 
((JavascriptExecutor) driver).executeScript("scroll(0,80);");
 
You need to figure out equivalent value of 80px here. As -80 is not equivalant to 80px.

 


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...