title: How can I tell that the page has finished loading?
body: <p>I'm playing with Chromium's headless web browser API. Based on chrome_remote_shell source code, I came up with the following code:</p>
  
  <pre><code>#!/usr/bin/env python
  
  import json
  import requests
  import pprint
  import websocket
  
  tablist = json.loads(requests.get("http://%s:%s/json" % ("localhost", 9222)).text)
  print(tablist)
  wsurl = tablist[0]['webSocketDebuggerUrl']
  conn = websocket.create_connection(wsurl)
  navcom = json.dumps({"id":0, "method":"Network.enable"})
  conn.send(navcom)
  navcom = json.dumps({"id":1, "method":"Page.navigate", "params":{"url":"https://news.ycombinator.com/"}})
  conn.send(navcom)
  
  while True:
      packet = json.loads(conn.recv())
      if 'method' in packet:
          print(packet['method'])
      else:
          print(packet)
  </code></pre>
  
  <p>Here's example output:</p>
  
  <pre><code>[{u'description': u'', u'title': u'Hacker News', u'url': u'https://news.ycombinator.com/', u'webSocketDebuggerUrl': u'ws://localhost:9222/devtools/page/7d03a57d-77a9-4ceb-b645-3b85461de5be', u'type': u'page', u'id': u'7d03a57d-77a9-4ceb-b645-3b85461de5be', u'devtoolsFrontendUrl': u'/devtools/inspector.html?ws=localhost:9222/devtools/page/7d03a57d-77a9-4ceb-b645-3b85461de5be'}]
  {u'id': 0, u'result': {}}
  Network.requestWillBeSent
  {u'id': 1, u'result': {u'frameId': u'21045.1'}}
  Network.responseReceived
  Network.dataReceived
  Network.dataReceived
  Network.loadingFinished
  Network.requestWillBeSent
  Network.requestWillBeSent
  Network.requestServedFromCache
  Network.responseReceived
  Network.dataReceived
  Network.loadingFinished
  Network.requestWillBeSent
  Network.requestServedFromCache
  Network.responseReceived
  Network.dataReceived
  Network.loadingFinished
  Network.requestWillBeSent
  Network.requestServedFromCache
  Network.responseReceived
  Network.dataReceived
  Network.loadingFinished
  Network.responseReceived
  Network.dataReceived
  Network.loadingFinished
  Network.requestWillBeSent
  Network.requestServedFromCache
  Network.responseReceived
  Network.dataReceived
  Network.loadingFinished
  </code></pre>
  
  <p>I noticed that I get a long stream of messages, last one of them being Network.loadingFinished, but I got this one for multiple requestIds. How can I modify my script so that it terminates when the page fully loaded and I can escape the loop?</p>