gpt4 book ai didi

C# Selenium Firefox - 错误异常 "Browsing context has been discarded"

转载 作者:太空宇宙 更新时间:2023-11-03 22:44:02 24 4
gpt4 key购买 nike

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

Browsing context has been discarded

我已经尝试了所有可能的方法,但最“震惊”的是我在 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();
}
}
}

最佳答案

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

我忘记在关闭不必要的选项卡后切换回“主”选项卡,我已经解决了添加 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

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.

关于C# Selenium Firefox - 错误异常 "Browsing context has been discarded",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50824371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com