Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium: don't wait for async resources

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?

like image 300
JustinLovinger Avatar asked Sep 05 '15 02:09

JustinLovinger


1 Answers

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:

  • How do you block Google Analytics from Selenium automated visits?

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

  • Disable images in Selenium Python
  • Do not want images to load and CSS to render on Firefox in Selenium WebDriver tests with Python
like image 115
alecxe Avatar answered Oct 11 '22 08:10

alecxe