I'm trying to parse content from URL's that contains a lot of Javascript generated content, for that I'm using WebBrowser control. Initially I had many issues when loading all the urls of my list of target urls in the same instance of the object, so I decided to dispose the object every few requests and then create a new instance and so on. The issue I'm facing now is that sometimes, when I Dispose the object, it opens a new IE browser window, independent from my application, loading the url's of the object that I've already disposed. Here is my code:
I saw a similar answer in this post, but it's not working for me.
Why is sometimes WebBrowser.Dispose() launching Internet Explorer?
private void TriggerNavigation ()
{
if (urlList.Count > 0)
{
progressBar1.Value++;
if (LoopUrls++ >= 1)
{
URL = string.Empty;
LoopUrls = 0;
timerAjaxLoad.Stop();
webBrowser1.Stop();
webBrowser1.AllowNavigation = false;
webBrowser1.Dispose();
webBrowser1 = null;
GC.Collect();
webBrowser1 = new WebBrowser();
}
URL = urlList.First();
label1.Text = "Processing.." + URL;
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.Navigate(URL);
webBrowser1.Navigating += webBrowser1_Navigating;
timerAjaxLoad.Start();
urlList.RemoveAt(0);
}
else
{
timerAjaxLoad.Stop();
}
}
UPDATE:
I figured out what was causing the issue. It was not happening for all the pages, but only for some with embedded JS that was triggering an external page. By catching the Navigating event and cancel the script calls to that page, it solved the issue.
void webBrowser1_Navigating (object sender, WebBrowserNavigatingEventArgs e)
{
foreach (HtmlElement x in ((WebBrowser)sender).Document.GetElementsByTagName("script"))
{
if (x.OuterHtml.Contains("survey"))
e.Cancel = true;
}
foreach (HtmlElement x in ((WebBrowser)sender).Document.GetElementsByTagName("iframe"))
{
e.Cancel = true;
}
}