-1

Can someone please help me how to get this code working?

String mvt = "1500";

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

As you can see I want to use the number in mvt in the execution script. For now, I get the error:

syntax error, insert ";" to complete Statement
String literal is not properly closed by a double-quote

at UsingActionsClass.UsingActions.main(UsingActions.java:23)

So, what will be the proper syntax, please?

Thank you.

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
blagojkk
  • 21
  • 1
  • 3
  • Possible duplicate of [How to scroll down using Selenium WebDriver with Java](https://stackoverflow.com/questions/16140337/how-to-scroll-down-using-selenium-webdriver-with-java) – JeffC Apr 29 '18 at 19:46

2 Answers2

0

You need to close the the quotes of the JavaScript snippet, and then the parenthesis of the call:

js.executeScript("window.scrollBy(0, arguments[0]);", mvt);
yong
  • 13,357
  • 1
  • 16
  • 27
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • You fixed one problem but `mvt` is undefined the way it's written. – JeffC Apr 29 '18 at 19:45
  • You are correct. The script is starting now but it is not scrolling. Can you please tell me what is the proper way to define mvt? – blagojkk Apr 29 '18 at 20:26
0

There are a couple issues. One is that you didn't close the quotes... that is what the error message is telling you. The second issue is that you aren't passing your mvt variable into the JS call. The corrected code is below.

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0, arguments[0])", mvt);
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • can i use 2 or more arguments? for example: String mvt = "1500"; String mvt2 = "3500"; //Scroll down JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("window.scrollBy(0, arguments[0])", mvt,mvt2); if possible what will be the proper syntax? – blagojkk Apr 30 '18 at 07:10