C#Selenium Firefox - 错误异常“浏览上下文已被丢弃”

时间:2018-06-12 19:20:58

标签: c# selenium-firefoxdriver

我正在编写一个帮助我的客户从网站下载发票PDF的程序,在我第一次使用drv.Navigate().GoToUrl(URL);时一切正常。之后,程序会休眠一段时间,当唤醒开始搜索我的客户的电子邮件时(使用S22 DLL),如果他找到某个电子邮件,则从e中提取链接邮件和使用(第二次)drv.Navigate().GoToUrl(URL);。但是这次我得到了一个例外

  

浏览上下文已被丢弃

我已经尝试了所有可能的事情,但最令人震惊的是我在Google上没有找到任何关于此错误的信息,也没有找到关于Selenium文档的信息。

我不明白是什么意思

我确定该链接有效,因为链接相同。

受此问题影响的代码

P.S:第一次下载是第二次下载完全

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

[编辑]

CloseUnnecessaryTabs()代码关闭每个打开的标签,只留下一个以避免Firefox关闭

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();
                    }
                }
            }

1 个答案:

答案 0 :(得分:2)

我在代码中发现了导致此异常的错误。

关闭不必要的标签后,我忘记切换回“主”标签,我已解决将drv.SwitchTo().Window(drv.WindowHandles[0]);添加到我的CloseUnnecessaryTabs()代码中。

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
            }

我找到了“提示”here

  

每个浏览上下文都有一个已知元素的关联列表。当浏览上下文被丢弃时,已知元素列表将随之丢弃。