0

I was trying to write a function either in Javascript or in Python using Selenium to calculate the page load time of a website. document.ready() will only give the DOM load time but there might be some AJAX calls which cannot be detected using document.ready().

There is even an extension in chrome web store named 'Page Load Time', which will calculate the total time, as per my requirements. How do I replicate same kind of functionality?

3 Answers3

1

You can use load like as follows.

$(window).load(function() {
    //code in here
});

See jQuery docs here. Also, another answer that will show you how to set up a page timer can be found here.

Paul Fitzgerald
  • 11,770
  • 4
  • 42
  • 54
  • Thanks for the response @PaulFitzgerald but how would I get the actual time using `window.load`? I need the total time taken to load a website, now this time metric will be used elsewhere in my code. – Divyanshu Juneja Jun 20 '18 at 02:52
0

driver.execute_script("return $.active == 0") should help you.

$.active returns the number of active Ajax requests. Link

Sahil Agarwal
  • 555
  • 3
  • 12
  • Hey thanks for the responce @sahil but will this work if the ajax requests are being executed inside some iframe of the website? `return $.active == 0` will be executed in the top of the web page if I'm not wrong. – Divyanshu Juneja Jun 20 '18 at 02:55
0

You can try it with selenium and with execute_script method, we will get information from window.performance.timing, it will return the milliseconds, but if we divide it by 1000 we will get the seconds of the loaded page.

from selenium import webdriver
driver=webdriver.Chrome()
driver.get("https://example.com")

load_time = driver.execute_script(
        """
        var loadTime = ((window.performance.timing.domComplete- window.performance.timing.navigationStart)/1000)+" sec.";
        return loadTime;
        """
        )

print(load_time)

Output

# something like this
0.803 sec.
Druta Ruslan
  • 7,171
  • 2
  • 28
  • 38
  • 1
    Thanks @zimdero but this would operate on the DOM of the page if I'm not wrong. I need to calculate not just the time take to load the DOM but also the time taken to complete AJAX calls as well, even if they are inside some iframe of the page. – Divyanshu Juneja Jun 20 '18 at 03:09