6

Selenium waits for async resource calls before proceeding to a new page.

Ex.

<script src="https://apis.google.com/js/platform.js" async defer></script>

On a website with many external apis (such as Google Analytics and share buttons from G+, Facebook, and Twitter). Selenium spends more time waiting for async calls than it does running tests.

Is there anyway to disable this behavior, so that selenium does not wait for async external api calls?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
JustinLovinger
  • 782
  • 7
  • 15

2 Answers2

5

What you see is page load timeout in action. You can tweak it and handle the timeout exception:

try:
    driver.set_page_load_timeout(5)  # in seconds
except TimeoutException:
    pass

# continue with testing

In addition to it, you can also add an Explicit Wait to wait for a certain desired "action" element to appear, so that you can proceed with your tests immediately once the element appears.


You can also optimize it by blocking requests to certain domains that are non-relevant for your tests and would not hurt the page rendering and would not affect your tests. For instance, if you want to block Google Analytics requests:

You can also disable images, CSS or flash (if this is applicable in your case):

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • This throws an error when the page takes too long to load. I want selenium to keep executing code and loading pages even if async resources are not loaded. – JustinLovinger Sep 05 '15 at 02:51
  • @PaintingInAir okay, what error are you seeing? Can you provide a reproducible example? Thanks. – alecxe Sep 05 '15 at 02:52
  • Setting a lower page load timeout results in a timeout exception. According to the selenium docs, this is the purpose of driver.set_page_load_timeout. The second part of your answer is on the right track. Since all the async resources I'm loading are external, it achieves the same effect, without explicitly addressing the problem of waiting for async calls. – JustinLovinger Sep 05 '15 at 03:55
  • @PaintingInAir good, but why don't handle and ignore the timeout exception and continue testing? Thanks! – alecxe Sep 05 '15 at 04:34
  • I think you're misunderstanding. I'm not getting timeout errors unless I reduce the page load timeout. I thought you were suggesting reducing the page load timeout to stop waiting for async resources. This is purely an issue of performance. – JustinLovinger Sep 05 '15 at 04:49
  • 1
    @PaintingInAir The "right" way to handle this is indeed to block the external resources call. That is (or should be) the point of the answer. The mechanics of how to accomplish that can be complicated, though I'd probably rely on a proxy. The "lazy" way to do it would be to set the page load timeout, then catch and ignore the exception then the timeout is hit. The piece that was missing from the answer was that if you go the timeout route, that you have to catch and ignore the exception. – JimEvans Sep 05 '15 at 07:33
0

with following code, you don't need wait to load complete page to make any process(i.e find_element functions)

FirefoxProfile fp = new FirefoxProfile();
fp.setPreference("webdriver.load.strategy", "unstable");
WebDriver driver = new FirefoxDriver(fp);

more details: https://code.google.com/p/selenium/wiki/FirefoxDriver#-Beta-_load_fast_preference

Mahsum Akbas
  • 1,523
  • 3
  • 21
  • 38