0

One problem with Selenium is that when a page makes heavy use of AJAX request, Selenium doesn't know when the request finishes and therefore when it should start querying the page for the requested elements.

My idea to solve this issue was to put an invisible div in the page that would contain a counter incremented each time an AJAX request finishes:

<!-- will be v1v after first AJAX requext, v2v after the second etc. -->
<div style="display: none;" id="ajaxcounter">v0v</div>

So in the Selenium testing code I can put lines like this:

WebDriverWait(self.driver, 10).until(
    text_to_be_present_in_element((By.ID, "ajaxcounter"), "v1v")
)
# test stuff that depends on the first AJAX request

However, the above line raises selenium.common.exceptions.TimeoutException, since apparently Selenium refuses to "see" the content of elements with style="display: none;" (if I remove this display: none; then Selenium works fine).

Is it possible to make Selenium see this one invisible element? It can complain about any other invisible elements normally, but still it should see just this one element.

  • 2
    Are you sure this isn't relevant to you: https://stackoverflow.com/questions/2835179/how-to-get-selenium-to-wait-for-ajax-response – Adam Jenkins Aug 24 '17 at 17:28

2 Answers2

1

you can use something like 'wait for ajax' to complete.

    public void WaitForAjax(int timeoutSecs = 10)
    {
        for (var i = 0; i < timeoutSecs; i++)
        {
            var ajaxIsComplete = (bool) _browser.ExecuteScript("return jQuery.active == 0");
            if (ajaxIsComplete) return;
            Thread.Sleep(100); //retry interval
        }
    }
0

You have multiple choices. I recommend this:

WebDriverWait(self.driver, 10).until(
    presence_of_element_located((By.XPATH, "//*[@id='ajaxcounter' and text()='v1v']"))
)

The Xpath will find the element that the id is 'ajaxcounter' and the text is 'v1v'.

Buaban
  • 5,029
  • 1
  • 17
  • 33