title: C# Selenium Firefox - Error Exception "Browsing context has been discarded"
body: <p>I'm writing a program who help my customer to download invoice PDF from a website, everything works well the first time I use <code>drv.Navigate().GoToUrl(URL);</code>. After that the program sleep for a certain amount of time and when awake starts to search through the e-mails of my customer (using <a href="https://github.com/smiley22/S22.Imap" rel="noreferrer">S22</a> DLL) and if he found a certain e-mail, extracts the link from the e-mail and use (for the 2nd time) <code>drv.Navigate().GoToUrl(URL);</code>. But this time I get an exception </p>
  
  <blockquote>
    <p>Browsing context has been discarded</p>
  </blockquote>
  
  <p>I've tried everything possible, but the most "shock" thing is that I don't find anything on Google about this error, nor on Selenium documentation.</p>
  
  <p>And I don't understand what means</p>
  
  <p>I'm sure that the link works, because is the same link.</p>
  
  <p>Below the code affected by this problem</p>
  
  <p>P.S: The first download is executed <strong>exactly</strong> as the 2nd download.</p>
  
  <pre><code>    public static int Go(string URL, ImapClient EmailClient, uint mUID, bool isFromOutlook) {
      // While the Firefox driver isn't initialized, wait for it
      while (isDrvInit != 1 &amp;&amp; isDrvInit != 2)
          Thread.Sleep(1);
          // If the Firefox driver was not able to initialize, we can't procede further
      if (isDrvInit == 2)
          return 0;
  
      try {
          drv.Navigate().GoToUrl(URL); // Here the program throw the exception
  
          if (isLoginPage()) {
              if (!Login()) {
                  if (Internet.IsAvailable()) {
                      Error.Show(Error.Code.MOBILCOM_LOGIN, Error.Status.F, Error.Type.DEFAULT,
                                "Unable to log-in to the Mobilcom account... Are the e-mail/password in the config file correct?");
                      } else {
                          Error.Show(Error.Code.FIREFOX_CANT_NAVIGATE, Error.Status.W, Error.Type.DEFAULT, String.Format(
                                    "Can't connect to Mobilcom because Internet connection is missing...", drv.Url));
                      }
  
                       return 0;
              } else {
                  Error.Show(Error.Code.MOBILCOM_LOGIN, Error.Status.S, Error.Type.DEFAULT,
                             "Successfully logged to the Mobilcom account!");
  
                  if (GetPdfInvoice() == true) {
                      if (isFromOutlook) {
                          MailMessage _m = EmailClient.GetMessage(mUID, true, Global.outlookSpecialFolder);
  
                          Error.Show(Error.Code._DEFAULT, Error.Status.S, Error.Type.OUTLOOK, String.Format(
                                    "PDF Invoice: Subject: [{0}] | Downloaded from the link '{1}' successfully saved! :)",
                                     _m.Subject, drv.Url));
                          } else {
                              MailMessage _m = EmailClient.GetMessage(mUID, true, Global.gmailSpecialFolder);
  
                              Error.Show(Error.Code._DEFAULT, Error.Status.S, Error.Type.GMAIL, String.Format(
                                         "PDF Invoice: Subject: [{0}] | Downloaded from the link '{1}' successfully saved! :)",
                                          _m.Subject, drv.Url));
                          }
                  } else {
                      if (!Internet.IsAvailable()) {
                          Error.Show(Error.Code.MOBILCOM_NO_INTERNET, Error.Status.W, Error.Type.DEFAULT, String.Format(
                                     "Can't download the PDF Invoice from '{0}' because Internet connection is missing!",
                                      drv.Url));
                          } else {
                              Error.Show(Error.Code.MOBILCOM_CANT_D_PDF, Error.Status.F, Error.Type.DEFAULT, String.Format (
                                        "Unknow Exception: Can't download the PDF Invoice from '{0}', retrying to download next time...",
                                         drv.Url));
                          }
                  }
  
                  CloseUnnecessaryTabs();
              }
          } else {
          // Still nothing
          }
  
          return 1;
      } catch {
          if (!Internet.IsAvailable()) {
              Error.Show(Error.Code.FIREFOX_CANT_NAVIGATE, Error.Status.W, Error.Type.DEFAULT, String.Format(
                        "Unable to continue on Mobilcom because Internet connection is missing, retrying to download next time..."));
          } else {
              Error.Show(Error.Code.FIREFOX_CANT_NAVIGATE, Error.Status.F, Error.Type.DEFAULT, String.Format(
                        "Unknow Exception: Unable to reach the '{0}' URL", drv.Url));
          }
  
          CloseUnnecessaryTabs();
          return 0;
      }
  }
  </code></pre>
  
  <p><strong>[EDIT]</strong></p>
  
  <p>The <code>CloseUnnecessaryTabs()</code> code closes every tab opened and leaves only one to avoid Firefox to be closed</p>
  
  <pre><code>private static void CloseUnnecessaryTabs() {
                  if (drv.WindowHandles.Count &gt; 1) {
                      for (int i = drv.WindowHandles.Count - 1; i &gt; 0; i--) {
                          drv.SwitchTo().Window(drv.WindowHandles[i]);
                          drv.Close();
                      }
                  }
              }
  </code></pre>