C# Selenium Firefox - 错误异常 "Browsing context has been discarded"
C# Selenium Firefox - Error Exception "Browsing context has been discarded"
我正在编写一个程序来帮助我的客户从网站下载发票 PDF,我第一次使用时一切正常 drv.Navigate().GoToUrl(URL);
。之后程序会休眠一段时间,醒来后开始搜索我客户的电子邮件(使用 S22 DLL),如果他找到了某个电子邮件,则提取 link 来自电子邮件并使用(第二次)drv.Navigate().GoToUrl(URL);
。但是这次我得到一个例外
Browsing context has been discarded
我已经尝试了所有可能的方法,但最 "shock" 的事情是我在 Google 和 Selenium 文档中都找不到任何关于此错误的信息。
我不明白什么意思
我确定 link 有效,因为 link.
受此问题影响的代码如下
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();
}
}
}
我在我的代码中发现了导致此异常的错误。
我忘记在关闭不需要的选项卡后切换回 "main" 选项卡,我已经解决了将 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
}
我找到了 "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.
我正在编写一个程序来帮助我的客户从网站下载发票 PDF,我第一次使用时一切正常 drv.Navigate().GoToUrl(URL);
。之后程序会休眠一段时间,醒来后开始搜索我客户的电子邮件(使用 S22 DLL),如果他找到了某个电子邮件,则提取 link 来自电子邮件并使用(第二次)drv.Navigate().GoToUrl(URL);
。但是这次我得到一个例外
Browsing context has been discarded
我已经尝试了所有可能的方法,但最 "shock" 的事情是我在 Google 和 Selenium 文档中都找不到任何关于此错误的信息。
我不明白什么意思
我确定 link 有效,因为 link.
受此问题影响的代码如下
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();
}
}
}
我在我的代码中发现了导致此异常的错误。
我忘记在关闭不需要的选项卡后切换回 "main" 选项卡,我已经解决了将 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
}
我找到了 "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.