Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have been running into intermittent errors with some java selenium-rc tests which I think are related to a page which has an ajax poll and automatically refreshes when some condition is reached on the server. In this scenario, I have no way of asking selenium to wait for the page to load, and so I run into a bunch of random "Couldn't access document.body" errors.

So, is there some way I can cause selenium to gracefully handle this situation? If not, is there some way I could detect whether the user is selenium from the page's javascript, and disable the automatic refresh?

If it helps at all, the javascript code in the page looks something like...

var ajax = new Ajax(url, { 
    update: state,
    method: 'get',
    onComplete: function(message) {
        if (some_condition) {
share|improve this question

In this scenario you could wait for the Ajax request to finish.

That way you don't concern yourself if the page is loaded. You continue after the ajax request is done.

You could do that this way:

            "selenium.browserbot.getCurrentWindow() == 0",

Where timeout is an optional integer parameter.

share|improve this answer
Was writing an answer as you posted this. I think that I'm going to see about implementing your solution in my own automation as this seems much more efficient. – Lyndon Vrooman May 6 '11 at 11:12
Would that just wait for the ajax request and the onComplete handler to finish, or would it wait until the new page (which is loaded by window.location.replace) is full loaded? – Matt Sheppard May 6 '11 at 11:12
@Lyndon: Thanks man. :) @Matt: This actually waits for EVERY jQuery request to finish. It waits until everything jQuery does on your page is done, even for a microsecond. If jQuery get's in the state: active=0 your program will continue. This of course is only valid for jQuery, but you can edit this to represent whatever tool you are using. Btw if you are not using jQuery, shame on you you should. :D – Hannibal May 6 '11 at 11:26
Thanks - I'll try it out. I'm still not sure it will work though, because I would have assumed that when the window.location.replace started loading a new page, the old jquery context would need to be destroyed. Assuming that happens, jquery wouldn't actually become active again unless the new page which is being loaded used it for something, and even then there would be points during the page loading where it was inactive. That said, I'm not sure so I'll test it out and see if you're right that survives through a page load. – Matt Sheppard May 8 '11 at 11:18
Yeah i hope it helps. It's just an idea after all. I really hope that will help you get from there at least. – Hannibal May 9 '11 at 7:08

If Hannibal's answer doesn't work, what has worked for me with pages that take a very long time to load, is adding calling a method either directly after the action which causes page to be refreshed, or, at the beginning of the next action, which checks to see if a particular element which will always be present on the page, is actually present.

share|improve this answer

The simple solution would be to have selenium execute a script to override/disable the javascript that self refreshes. I can't remember if selenium rc is selenium 1 or selenium 2, but it rexecuting javascript can be executed from both drivers. That aside, @Hannibal has a pretty good solution, and I would most likely do it that way myself

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.