我们在我们的网站上添加了一个异步javascript调用。我试图让SeleniumWebdriver等待调用的响应。
侦听器如下所示:
$(document).on("application:subapp:rendered", function(){console.log("foo");});
Mywebdriver代码(python):
driver.set_script_timeout(30)
response = driver.execute_async_script("$(document).on(\"application:subapp:rendered\", function(){return \"foo\";});"
下一步我执行的页面应该使“foo”返回
但这是我的回应。。。
TimeoutException: Message: asynchronous script timeout: result was not recei ved in 30 seconds (Session info: chrome=41.0.2272.118) (Driver info: chromedriver=2.11.298604 (75ea2fdb5c87f133a8e1b8da16f6091fb7d532 1e),platform=Windows NT 6.1 SP1 x86_64)
使用
arguments[0]
作为回调:另见(应有助于理解):
当您调用
execute_async_script
时,Selenium会将您必须调用的回调作为最后一个参数传递给JavaScript代码,以指示异步代码已完成执行,如果您在调用execute_async_script
时没有在脚本后传递参数,则这将作为JavaScript中的arguments[0]
来访问。无论传递给这个回调的值是什么,您的execute_async_script
都将返回这样的值:在上面的代码中,我将回调分配给
done
。我就是喜欢这样做的。注意如何通过调用done("foo")
来设置response
要设置的值。还要注意,我使用的是
.one()
,而不是.on()
。我发现Selenium(至少2.45)从不认为为execute_async_script
创建的旧回调是“过时的”,因此如果在上面的JavaScript完成执行后,您的事件有可能再次发生,那么它将再次调用回调,Selenium将再次执行该调用。如果此时恰好有另一个execute_async_script
正在运行,则此虚假调用将终止返回值为“foo”的另一个execute_async_script
调用。在我的一个测试套件里发生过这种事。它导致了非常奇怪的失败。相关问题 更多 >
编程相关推荐