Selenium和异步JavaScript调用 [英] Selenium and asynchronous JavaScript calls

查看:103
本文介绍了Selenium和异步JavaScript调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Selenium和JavaScript回调函数都很陌生,我有一个很大的问题,我无法自己解决。我需要使用JavaScript的指定变量。如果我使用GoogleChrome打开页面并使用控制台输入我的JavaScript代码,我可以使用以下内容获取变量:

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;

如果我一步一步地输入这一行,我很容易得到正确的值myId。但是,当然,如果我尽可能快地执行三行,我得到null作为返回值,因为当我返回myId时回调函数没有完成。 SOOOO ..如果我像这样使用selenium:

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;");

我只得到null作为结果。所以...不知何故,我必须等待回调函数,直到我返回myId。我是否必须使用executeAsyncScript以及如何使用?我坐了几个小时,尝试了不同的东西,但我找不到答案。

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.

提前感谢您的帮助!

推荐答案

对于异步代码你必须使用 executeAsyncScript

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);" +
            "}" +
        ");");

使用 executeAsyncScript 调用的脚本将具有一个回调添加到传递给它的参数列表中。由于您没有向脚本传递任何参数,因此 arguments [0] 包含回调。您的代码必须在完成工作后调用此回调。您为回调赋予的值是 executeAsyncScript 返回的值。

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.

在上面的代码中,我是通过将其置于匿名函数中来拼写对 done 的调用,但代码可以更简洁地编写为:

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);");

甚至:

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

这篇关于Selenium和异步JavaScript调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆