我想实现脚本执行时间处理,但偶然发现了这个问题。如果我设计webdriver的脚本以下面的方式执行,它会成功返回一个变量,但它不会触发ScriptTimeoutException。有什么想法吗?我从webdriver's javadoc示例
采用了这个脚本System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//---setting script timeout to 1ns to force ScriptTimeoutException
driver.manage().timeouts().setScriptTimeout(1, TimeUnit.NANOSECONDS);
//this script works fine, ScriptTimeoutException is triggered
String script1 = "window.setTimeout(arguments[arguments.length - 1], 500);";
//this script is able to pass his return variable back to Java, but doesn't triggers ScriptTimeoutException
String script2 = "var callback = arguments[arguments.length - 1];" +
"var stringVar = 'abcd';" +
"callback(stringVar);";
while (true) {
Instant beforeScript = Instant.now();
//((JavascriptExecutor) driver).executeAsyncScript(script1);
String result = (String) ((JavascriptExecutor) driver).executeAsyncScript(script2);
System.out.println(result + " " + Duration.between(beforeScript, Instant.now()).toMillis());
}
答案 0 :(得分:0)
好吧,在用selenium \ chromedriver开发人员搜索了一段时间并discussing之后,我发现我的脚本实际上并不是异步脚本。
将抛出ScriptTimeoutException的正确异步脚本将是这样的:
var callback = arguments[arguments.length - 1];
var stringVar = 'abcd';
setTimeout(()=>callback(stringVar), 100);