简体   繁体   English

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

[英]C# Selenium Firefox - Error Exception “Browsing context has been discarded”

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); 我正在编写一个程序,该程序可以帮助我的客户从网站上下载发票PDF,第一次使用drv.Navigate().GoToUrl(URL);时一切正常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); 之后,程序会休眠一段时间,当清醒时开始搜索我的客户的电子邮件(使用S22 DLL),如果他找到了某个电子邮件,则从电子邮件中提取链接并使用(第二次) 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. 我已经尝试了所有可能的方法,但是最“令人震惊”的事情是我在Google上找不到关于此错误的任何信息,也没有在Selenium文档中找到任何信息。

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 下面的代码受此问题影响

PS: The first download is executed exactly as the 2nd download. PS:第一次下载与第二次下载完全相同

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

I have found the bug in my code who 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]); 关闭了不必要的选项卡后,我忘记了切换回“主”选项卡,我解决了添加drv.SwitchTo().Window(drv.WindowHandles[0]); to my CloseUnnecessaryTabs() code. 到我的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
            }

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. 当浏览上下文被丢弃时,已知元素的列表将与之一起被丢弃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM