最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to get Asynchronous Javascript responses from Selenium Webdriver - Stack Overflow

programmeradmin0浏览0评论

We have added an asynchronious javascript call to our website. I'm trying to get Selenium Webdriver to wait for a response from the call.

The listener looks like this:

$(document).on("application:subapp:rendered", function(){console.log("foo");});

My webdriver code (python):

driver.set_script_timeout(30)
response =  driver.execute_async_script("$(document).on(\"application:subapp:rendered\", function(){return \"foo\";});"

Next I execute the page should make "foo" return

however this is my response...

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)

We have added an asynchronious javascript call to our website. I'm trying to get Selenium Webdriver to wait for a response from the call.

The listener looks like this:

$(document).on("application:subapp:rendered", function(){console.log("foo");});

My webdriver code (python):

driver.set_script_timeout(30)
response =  driver.execute_async_script("$(document).on(\"application:subapp:rendered\", function(){return \"foo\";});"

Next I execute the page should make "foo" return

however this is my response...

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)

Share Improve this question edited Apr 8, 2015 at 14:35 Saifur 16.2k7 gold badges50 silver badges74 bronze badges asked Apr 8, 2015 at 14:33 user3249517user3249517 1311 gold badge1 silver badge6 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12

When you call execute_async_script, Selenium passes as the last argument to the JavaScript code the callback you must call to indicate that the asynchronous code is done executing, if you do not pass arguments after your script when you call execute_async_script, then this will be accessible as arguments[0] in the JavaScript. Whatever value you pass to this callback is what your execute_async_script will return so:

response = driver.execute_async_script("""
    var done = arguments[0];
    $(document).one('application:subapp:rendered', 
        function(){
           done('foo');
    });
""")

In the code above I assign the callback to done. That's just the way I like to do it. Note how the value to which response will be set is set by calling done("foo").

Note also that I'm using .one() rather than .on(). I've discovered that Selenium (at least up to 2.45) does not ever consider an old callback created for execute_async_script to be "obsolete" so if there is any chance that your event can happen again after your JavaScript above has finished executing, then it will call the callback again, and Selenium will honor the call again. If you happen to have another execute_async_script running at that time, then this spurious call will terminate your other execute_async_script call with the return value "foo". I've had that happen in one of my test suites. It led to very bizarre failures.

Use arguments[0] as a callback:

driver.execute_async_script("""
    $(document).on('application:subapp:rendered', arguments[0]);
""")

See also (should help in understanding):

  • Understanding execute async script in Selenium
发布评论

评论列表(0)

  1. 暂无评论