From 64431fa9fb19e0e27057a66d82ac6eaa776ad3a0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 31 Mar 2013 08:45:11 +0530 Subject: [PATCH] Add API to start load and wait till DOM is ready --- src/calibre/web/jsbrowser/browser.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/calibre/web/jsbrowser/browser.py b/src/calibre/web/jsbrowser/browser.py index be3174bdb9..114e0b06df 100644 --- a/src/calibre/web/jsbrowser/browser.py +++ b/src/calibre/web/jsbrowser/browser.py @@ -92,6 +92,10 @@ class WebPage(QWebPage): # {{{ def on_unsupported_content(self, reply): self.log.warn('Unsupported content, ignoring: %s'%reply.url()) + @property + def ready_state(self): + return unicode(self.mainFrame().evaluateJavaScript('document.readyState').toString()) + # }}} class ProxyFactory(QNetworkProxyFactory): # {{{ @@ -368,6 +372,29 @@ class Browser(QObject, FormsMixin): self.page.mainFrame().load(QUrl(url)) return self._wait_for_load(timeout, url) + @property + def dom_ready(self): + return self.page.ready_state in {'complete', 'interactive'} + + def wait_till_dom_ready(self, timeout=30.0, url=None): + start_time = time.time() + while not self.dom_ready: + if time.time() - start_time > timeout: + raise Timeout('Loading of %r took longer than %d seconds'%( + url, timeout)) + self.run_for_a_time(0.1) + + def start_load(self, url, timeout=30.0): + ''' + Start the loading of the page at url and return once the DOM is ready, + sub-resources such as scripts/stylesheets/images/etc. may not have all + loaded. + ''' + self.current_form = None + self.page.mainFrame().load(QUrl(url)) + self.run_for_a_time(0.01) + self.wait_till_dom_ready(timeout=timeout, url=url) + def click(self, qwe_or_selector, wait_for_load=True, ajax_replies=0, timeout=30.0): ''' Click the :class:`QWebElement` pointed to by qwe_or_selector.