【问题标题】:Why does 'executeAsyncScript (window.setTimeout (5000))' function in Java for selenium is holding the next function to execute after 5 seconds?为什么 Selenium 的 Java 中的“executeAsyncScript (window.setTimeout (5000))”函数在 5 秒后保持下一个要执行的函数?
【发布时间】:2019-11-05 06:55:38
【问题描述】:

我想使用异步函数调用来异步运行几个脚本,但我无法使用 javaScriprtExecutor 的 executeAsyncScript() 函数来实现它。 这里有一个小sn-p供大家参考。

我相信函数“executeAsyncScript”应该异步运行,并且第 2 行的函数调用(本质上更快)应该在 executeAsyncScript() 之前完成。但这没有发生。仅当函数 1 的执行结束(包括 5 秒延迟)时才调用第 2 行的函数调用!

谁能帮我理解我在这里看不到什么?

1. js.executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 5000);");
2. driver.findElement(By.xpath("//input")).sendKeys("Search term!!");

使用此序列,浏览器等待 5 秒,然后执行 sendkeys()。

【问题讨论】:

标签: javascript selenium selenium-webdriver


【解决方案1】:

execteAsyncScript 和 execteScript 两个函数都会阻塞 WebDriver 控制流,直到它们完成。这就是延迟 5 秒的原因。

完整的解释请看这里 WebDriver executeAsyncScript vs executeScript

【讨论】:

  • 那么我怎样才能异步运行这样的场景呢?有没有更好的实时方式使用 executeAsyncScript 让脚本运行得更快?
  • 如果您根据问题使用 javascript 执行器等待,那么您可以尝试 webdriver 等待。
  • 这是为了理解异步是如何工作的。想看一个示例代码来真正看到异步发生的事情。因此,这几行代码。与 Webdriver 无关等待。我认为等待和任何其他后续功能可能是看到它发生的最佳方式,但它只是没有用。你有任何样品可以分享吗?提前致谢。
  • 如果我要求 wat 的 AsyncScript 5000 毫秒,它将等待那个时间。这里异步并不意味着在没有响应之前跳转命令。请参考-automationbasicsselenuim.blogspot.com/2019/08/…
猜你喜欢
  • 2015-09-11
  • 1970-01-01
  • 2014-10-17
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-05
相关资源
最近更新 更多