title: Python Selenium Detect when the browser is closed
body: <p>Right now, I use this as a way to detect when the user closes the browser:</p>
  
  <pre><code>while True:
      try:
          # do stuff
      except WebDriverException:
          print 'User closed the browser'
          exit()
  </code></pre>
  
  <p>But I found out that it is very unreliable and a very bad solution since <code>WebDriverException</code> catches a lot of exception (if not all) and most of them are not due to the user closing the browser.</p>
  
  <p>My question is: <em>How to detect when the user closes the browser?</em></p>