【问题标题】:C# Selenium Firefox - Error Exception "Browsing context has been discarded"C# Selenium Firefox - 错误异常“浏览上下文已被丢弃”
【发布时间】:2018-11-22 06:38:20
【问题描述】:

我正在编写一个程序来帮助我的客户从网站下载发票 PDF,我第一次使用 drv.Navigate().GoToUrl(URL); 时一切正常。之后程序会休眠一段时间,然后在唤醒时开始搜索我的客户的电子邮件(使用S22 DLL),如果他找到了某个电子邮件,则从电子邮件中提取链接并使用(第二次)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();
                    }
                }
            }

【问题讨论】:

    标签: c# selenium-firefoxdriver


    【解决方案1】:

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

    我在关闭不必要的标签后忘记切换回“主”标签,我已经解决了将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

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

    【讨论】:

    • 赞美你,先生!在我设法找到此页面之前,我遇到了同样的问题并且找不到任何解决方案。这是我的量角器测试中的问题。它实际上跨越了单独的测试。尽管在一次测试结束时,我不需要返回主窗口,但我显然需要这样做是为了下一次测试。
    猜你喜欢
    • 1970-01-01
    • 2019-06-28
    • 2019-06-28
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 2015-05-27
    相关资源
    最近更新 更多