java – 无法执行Selenium异步脚本

前端之家收集整理的这篇文章主要介绍了java – 无法执行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);

测试页面包含以下脚本:

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

这引发了一个例外:

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)

如果我从我的脚本中删除setTimeout,并且我调用回调,它可以工作.但这不是我想要的.

谢谢.

解决方法

它非常奇怪,API在2 ms内超时.

我猜测脚本超时是以某种方式配置不正确(< = 0sec).由于您的窗口超时发生在3秒钟之后,请尝试将脚本超时设置为大于3秒的值,然后再进行通话. 像这样:

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

这可能会起作用

原文链接:https://www.f2er.com/java/123159.html

猜你在找的Java相关文章