java 无法执行 Selenium 异步脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11646586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:54:27  来源:igfitidea点击:

Unable to execute Selenium asynchronous script

javaselenium-webdriver

提问by redochka

Why am I getting an exception with selenium 2.25.0 when trying to execute asynchronous script.

为什么我在尝试执行异步脚本时遇到 selenium 2.25.0 异常。

//navigate to my test page.

String script = "var callback = arguments[arguments.length - 1];"  +
                "getResult(callback)";

Object result = ((JavascriptExecutor)driver).executeAsyncScript(script, "");

System.out.println(result);

The test page contains the following script:

测试页包含以下脚本:

var result = true;
function getResult(callback){
    window.setTimeout(function(){callback(true);}, 3000);
}

This throws an exception:

这会引发异常:

FAILED: testSeleniumAsync
org.openqa.selenium.TimeoutException: Script execution failed. Script: var callback = arguments[arguments.length - 1];getResult(callback);
 Timed out waiting for asyncrhonous script result after 2 ms (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 11 milliseconds
Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 22:18:01'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '3.2.0-27-generic', java.version: '1.6.0_26'
Driver info: driver.version: RemoteWebDriver
Session ID: 6347b507cf22b6c2d3312937a82a0a02
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)

If I remove the setTimeout from my script and I call callback it works. But this is not what I want.

如果我从我的脚本中删除 setTimeout 并调用回调,它会起作用。但这不是我想要的。

Thanks.

谢谢。

回答by Ashwin Prabhu

Its very strange that the API is timing out in 2 ms.

API 在 2 毫秒内超时,这很奇怪。

I am guessing that the script time out is somehow incorrectly configured (<= 0sec). Since your window timeout occurs after 3 secs, try setting the script time out to some value greater than 3 secs, before you make the call.

我猜脚本超时在某种程度上配置不正确(<= 0sec)。由于您的窗口超时发生在 3 秒后,请尝试将脚本超时设置为大于 3 秒的某个值,然后再进行调用。

Like so:

像这样:

driver.manage().timeouts().setScriptTimeout(5, TimeUnit.SECONDS);

That might work.

那可能会奏效。