Home > Software engineering >  Scrolling through a section throws error as null cannot create object
Scrolling through a section throws error as null cannot create object

Time:01-22

For scrolling through a section, I wrote below code, JavaScriptExecutor js= (JavaScriptExecutor) driver;. js.executeScript (scroll code); but here executeSctipt method is autosuggested like js.executeScript(null,args) and it throw error as null cannot create object. The actual argument (scroll code) is working as expected in the console.

It should be displayed as js.executeScript(arg0,arg1);

CodePudding user response:

The error "null cannot create object" is occurring because the first argument of the executeScript() method is expecting a string, which represents the JavaScript code that you want to execute. However, you are passing null as the first argument, which is causing the error.

You need to pass a string containing the JavaScript code as the first argument, like this:

js.executeScript("window.scrollBy(0,arguments[0])", 200);

This will scroll the page down by 200 pixels.

You can also pass multiple arguments like this:

js.executeScript("window.scrollBy(arguments[0],arguments[1])", x, y);

This will scroll the page by x pixels horizontally and y pixels vertically.

So, in your case it should be like this:

js.executeScript(arg0,arg1);

where arg0 is the javascript code, and arg1 is the argument you want to pass to that javascript code.

Make sure to replace arg0 and arg1 with the correct javascript code and arguments.

Also, you can check the javascript code you are passing is correct or not by running it on browser's developer console first. This will help you to verify if there are any errors in your javascript code.

CodePudding user response:

If the JavaScript code you're passing to the executeScript() method is working as expected in the browser console, then the issue is likely with the way the method is being called within your code.

It's possible that the executeScript() method is being passed null as the first argument due to an issue with the template or the way the code is being generated in Eclipse.

Here are a few things you can try to resolve the issue:

  1. Check the value of the scrollCode variable before passing it to the executeScript() method. Make sure it is not null or an empty string.

  2. Verify that the JavaScript code you're passing to the executeScript() method is a string and not any other type.

  3. Check that there are no syntax errors in the JavaScript code, such as unclosed quotes or missing semicolons, that might be causing the executeScript() method to interpret the code as null.

  4. Verify that the executeScript() method is being called with the correct arguments, and that there are no typos or errors in the method call.

  5. Try to run the JavaScript code in a standalone script outside of your project to see if the problem is with the project or the code.

If you're still unable to resolve the issue, please let me know the exact error message and the version of selenium you are using, as it might help to better understand the problem and provide a solution.

  • Related