硒和asynchronos JavaScript调用 [英] Selenium and asynchronos JavaScript calls

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

问题描述

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

  1。 var myId; 
2. getSomeIdStuffInfo(somestuff,function(docId)(myId = docId));
3. return myId;

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

  JavascriptExecutor js =(JavascriptExecutor)驱动程序; 
String docInfoVal =(String)js.executeScript(+
var myId;+
getCurrentDocumentInfo(\somestuff\,+
function (docId){+
myId = docId;+
}+
);+
return myId;);

我只会得到空值。所以......不知何故,我必须等待回调函数,直到我返回myId。我必须使用executeAsyncScript吗?我坐了几个小时,尝试了不同的事情,但我找不到答案。



预先感谢您的帮助! 对于异步代码你必须使用 executeAsyncScript

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

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



在上面的代码中,通过将 done 放入一个匿名函数中,但可以更简洁地编写代码:

  JavascriptExecutor js =(JavascriptExecutor)驱动程序; 
String docInfoVal =(String)js.executeAsyncScript(+
var done = arguments [0];+
getCurrentDocumentInfo(\somestuff\,done); );

甚至:

  JavascriptExecutor js =(JavascriptExecutor)驱动程序; 
String docInfoVal =(String)js.executeAsyncScript(
getCurrentDocumentInfo('somestuff',arguments [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!

解决方案

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

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

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