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

查看:15
本文介绍了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 .. 如果我像这样使用硒:

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天全站免登陆