The main objective is to retrieve the JavaScript element authKey in Java code. Below are the codes:
index.html:
...
<script type="text/javascript">
var params = {
authKey: "abc"
};
Main.init("#content", params);
</script>
...
main.java:
public static void main(String[] args) throws InterruptedException {
WebDriver d = new FirefoxDriver();
d.get("***/index.html");
// System.out.println(" var " + d.findElement(By.xpath("/html/body/script[3]")));
Thread.sleep(3000);
JavascriptExecutor jse = (JavascriptExecutor) d;
// System.out.println(jse.executeAsyncScript("document.URL"));
Object val = jse.executeScript("return params.authKey;");
d.quit();
}
Each time, I am encountering an issue like this:
Exception in thread "main" org.openqa.selenium.JavascriptException: ReferenceError: **params is not defined**
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: host: 'Mikhails-MacBook-Pro.local', ip: '10.10.20.139', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.11.6', java.version: '1.8.0_111'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
I have attempted various methods to retrieve the authKey parameter, without using return or as a function, yet nothing seems to work. Can anyone provide assistance with this?
Thank you in advance.
UPD: The solution is effective, but I am still interested in finding a native JS solution specifically for obtaining the params.authKey parameter, as there may be numerous params in the future.