简体   繁体   English

WebBrowser启动Internet Explorer

[英]WebBrowser launch internet explorer

I'm trying to parse content from URL's that contains a lot of Javascript generated content, for that I'm using WebBrowser control. 我正在尝试从URL中解析包含很多Javascript生成内容的内容,为此我正在使用WebBrowser控件。 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. 最初,在同一对象的实例中加载目标URL列表的所有URL时,我遇到很多问题,因此我决定每隔几个请求就处理一次对象,然后创建一个新的实例,依此类推。 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. 我现在面临的问题是,有时,当我处理该对象时,它会打开一个新的IE浏览器窗口,与我的应用程序无关,并加载我已经处理过的对象的url。 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? 为什么有时WebBrowser.Dispose()启动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. 并非所有页面都发生这种情况,而是仅发生在某些触发了外部页面的嵌入式JS中。 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;
        }

    }  

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. 并非所有页面都发生这种情况,而是仅发生在某些触发了外部页面的嵌入式JS中。 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;
        }

    }  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM