Skip to content
Advertisement

javascript execution failing in java with XPathResult undefined

I’m trying to execute the javascript function with java, and I’m getting an error message that it is not able to find some of the classes. can someone please help me to clear this issue?

My Java class

JavaScript

Exception

JavaScript

please let me know if data required from my side.

Advertisement

Answer

The problem with your code is, you are using XPathResult which present under window object, window object implementation is provided by the web browser. Nashorn (ScriptEngine) doesn’t provide window implementation.

From Java Docs:

While Oracle Nashorn runs ECMA-compliant JavaScript, it is important to note that objects normally accessible in a web browser are not available, for example, console, window, and so on.

You can execute and verify your js code with Nashorn using jjs option:

JavaScript

As you want to parse the HTML file, you can achieve it by using HTML parser such as https://jsoup.org/ , if are using this code for testing you can use Selenium’s API JavascriptExecutor#executeScript(...)

Advertisement