【发布时间】: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();
}
}
}
【问题讨论】: