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);
. 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);
. 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.
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
P.S: The first download is executed exactly as the 2nd download.
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
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 programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
I have found the bug in my code which 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]);
to my CloseUnnecessaryTabs()
code.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With