最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

function - Selenium and asynchronous JavaScript calls - Stack Overflow

programmeradmin0浏览0评论

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!

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!

Share Improve this question edited Sep 22, 2018 at 10:42 nisarg parekh 4374 silver badges23 bronze badges asked Jul 3, 2015 at 14:09 spcialspcial 1,57919 silver badges46 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

For asynchronous code you have to use executeAsyncScript:

JavascriptExecutor js = (JavascriptExecutor) driver; 
String docInfoVal = (String) js.executeAsyncScript("" +
        "var done = arguments[0]; " +
        "getCurrentDocumentInfo(\"somestuff\"," +
            "function(docId) {" +
                "done(docId);" +
            "}" +
        ");");

The script you call with executeAsyncScript will have a callback added to the list of arguments passed to it. Since you pass no arguments to your script, then arguments[0] contains the callback. Your code must call this callback when it is done working. The value you give to the callback is the value that executeAsyncScript returns.

In the code above, I've spelled out the call to done by putting it in an anonymous function but the code could be written more concisely as:

JavascriptExecutor js = (JavascriptExecutor) driver; 
String docInfoVal = (String) js.executeAsyncScript("" +
        "var done = arguments[0]; " +
        "getCurrentDocumentInfo(\"somestuff\", done);");

Or even:

JavascriptExecutor js = (JavascriptExecutor) driver; 
String docInfoVal = (String) js.executeAsyncScript(
        "getCurrentDocumentInfo('somestuff', arguments[0]);");

Though this is almost same as what @Louis said.You have to set the setScriptTimeout beforehand for the script to pass.

The default timeout for a script to be executed is 0ms. In most cases, including the examples below, one must set the script timeout WebDriver.Timeouts.setScriptTimeout(long, java.util.concurrent.TimeUnit) beforehand to a value sufficiently large enough.

Below is an example for which I'm waiting for 10 seconds to return a string

  driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);//important

    JavascriptExecutor executor = (JavascriptExecutor) driver;
    String val = (String) executor.executeAsyncScript(""
            + "var done=arguments[0]; "
            + "setTimeout(function() {"
            + "   done('tada');"
            + "  }, 10000);");

    System.out.println(val);
发布评论

评论列表(0)

  1. 暂无评论