i'm quite new to Selenium and JavaScript-callback functions and i have a big issue I can't solve myself. I need a specified variable using JavaScript. If I open the page with GoogleChrome and use the console to enter my JavaScript-Code I can get the variable using something like this:
1. var myId;
2. getSomeIdStuffInfo("somestuff",function(docId)(myId = docId));
3. return myId;
If I enter this lines step by step I easily get the correct value myId. But, of course, if i execute the three lines as fast as possible, i get null as return value because the callback function is not finished when i return myId. SOOOO.. if I use selenium like this:
JavascriptExecutor js = (JavascriptExecutor) driver;
String docInfoVal = (String) js.executeScript("" +
"var myId; " +
"getCurrentDocumentInfo(\"somestuff\"," +
"function(docId) {" +
"myId = docId;" +
"}" +
");" +
"return myId;");
I only get null as result. So... somehow I have to "wait" for the callback function until i return myId. Do I have to use executeAsyncScript and how? I'm sitting on it for hours and tried different things but I just can't find the answer.
Thanks in advance for any help!