5

I'm writing a program who help my customer to download invoice PDF from a website, everything works well the first time I use drv.Navigate().GoToUrl(URL);. 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 S22 DLL) and if he found a certain e-mail, extracts the link from the e-mail and use (for the 2nd time) drv.Navigate().GoToUrl(URL);. But this time I get an exception

Browsing context has been discarded

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.

And I don't understand what means

I'm sure that the link works, because is the same link.

Below the code affected by this problem

P.S: The first download is executed exactly as the 2nd download.

    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 && 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;
    }
}

[EDIT]

The CloseUnnecessaryTabs() code closes every tab opened and leaves only one to avoid Firefox to be closed

private static void CloseUnnecessaryTabs() {
                if (drv.WindowHandles.Count > 1) {
                    for (int i = drv.WindowHandles.Count - 1; i > 0; i--) {
                        drv.SwitchTo().Window(drv.WindowHandles[i]);
                        drv.Close();
                    }
                }
            }
Mutu A.
  • 248
  • 3
  • 17

1 Answers1

11

I have found the bug in my code which caused this exception.

I have forgot to switch back to the "main" tab after closing the unnecessary tabs, I have solved adding drv.SwitchTo().Window(drv.WindowHandles[0]); to my CloseUnnecessaryTabs() code.

private static void CloseUnnecessaryTabs() {
                if (drv.WindowHandles.Count > 1) {
                    for (int i = drv.WindowHandles.Count - 1; i > 0; i--) {
                        drv.SwitchTo().Window(drv.WindowHandles[i]);
                        drv.Close();
                    }
                }

                drv.SwitchTo().Window(drv.WindowHandles[0]); // <-- The solution
            }

I have found a "hint" here

Each browsing context has an associated list of known elements. When the browsing context is discarded, the list of known elements is discarded along with it.

Elmue
  • 7,602
  • 3
  • 47
  • 57
Mutu A.
  • 248
  • 3
  • 17
  • Praise you, sir! I was having this same issue and could not find ANY solution until I managed to find this page. This was the issue in my protractor tests. It actually spanned separate tests. Even though, at the end of one test, I had no need to go back to the main window, I apparently needed to do so for the benefit of the next test. – Reverend Bubbles Jul 09 '18 at 19:42