2013-08-23 60 views
2

我正在使用Visual Studio C#2010的Web瀏覽器。c#Web瀏覽器保持凍結,但常規的IE9瀏覽器不會

web瀏覽器1導航到這個鏈接:

http://www.costco.com/IOGEAR-Wireless-1080p-HDMI-Transmitter-and-Receiver-3D-Compatible-2x-HDMI-Ports.product.100011675.html

當它到達的頁面,它加載和凍結。

我不認爲網頁有什麼問題,因爲chrome,firefox和常規IE9根本不會凍結。

只有我的c#程序中的Web瀏覽器在導航到此鏈接時會凍結。

如何防止凍結?該網頁似乎是從另一個網站調用一些html數據。

我試圖將此代碼添加到我的程序

this.webBrowser1.ScriptErrorsSuppressed = TRUE;

我還更改了網絡瀏覽器的註冊表值,以便它將使用Internet Explorer版本9,到目前爲止,這兩個都不起作用。

這是我使用

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
    { 
     InitializeComponent(); 

     webBrowser1.ScriptErrorsSuppressed = true; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     webBrowser1.Navigate("http://www.costco.com/IOGEAR-Wireless-1080p-HDMI-Transmitter-and-Receiver-3D-Compatible-2x-HDMI-Ports.product.100011675.html"); 
    } 

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
    { 

     } 
    } 
} 
+2

分享你的代碼 – Arshad

+0

好吧,我分享了它 –

+0

您可能會檢查WebBrowser控件是否實際使用IE瀏覽器相同版本的您的Internet Explorer是。 (聽起來很奇怪,但忍受着我。)使用它們訪問可以檢測瀏覽器版本的頁面(例如http://www.whatismybrowser.com/)。這可能是因爲webbrowser控件使用的是舊版本的IE ......您可以在註冊表中修改它。看到這裏:http://stackoverflow.com/questions/5531452/webbrowser-control-to-use-ie9和在這裏:http://stackoverflow.com/questions/4612255/regarding-ie9-webbrowser-control – GojiraDeMonstah

回答

5

的問題是不是與WebBrowser控件本身的代碼,它是與特定的網站是如何試圖執行一些JavaScript是陷在一個循環。

地比較和對比:

1)將網址更改爲http://google.com。工作正常。

2)現在。爲Navigating事件添加一個事件處理程序。喜歡的東西:

this.webBrowser1.Navigating += new System.Windows.Forms.WebBrowserNavigatingEventHandler(this.webBrowser1_Navigating); 

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) 
{ 
    Console.WriteLine("Navigating to: " + e.Url); 
} 

你會看到有一個不斷嘗試重定向頁面的JavaScript函數。下面是我的控制檯輸出顯示出來(就是無限期地):

Navigating to: javascript:void((function(){document.open();document.domain='costco.com';document.write('<!DOCTYPE html>');document.close();})()) 
Navigating to: about:blank 
Navigating to: javascript:void((function(){document.open();document.domain='costco.com';document.write('<!DOCTYPE html>');document.close();})()) 
Navigating to: about:blank 
Navigating to: javascript:void((function(){document.open();document.domain='costco.com';document.write('<!DOCTYPE html>');document.close();})()) 
Navigating to: about:blank 
Navigating to: javascript:void((function(){document.open();document.domain='costco.com';document.write('<!DOCTYPE html>');document.close();})()) 

這使得WebBrowser控件本質上無法使用。

編輯: 好,在解決辦法一個刺(這可能是可怕的,但它是令人沮喪的是,奇怪的重定向循環僅在WebBrowser控件的瀏覽器發生)。

如果在另一個導航事件完成之前阻止導航事件被調用,它將加載該頁面並且不凍結,並且鏈接似乎可以正常工作。它是這樣的:

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e) 
    { 
     Console.WriteLine("Navigated to: " + e.Url); 
     isNavigating = false; 
     webBrowser1.AllowNavigation = true; 
    } 

    bool isNavigating = false; 
    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) 
    { 
     if (isNavigating && e.Url.ToString().Contains("javascript:void((function(){document.open();document.domain='costco.com'")) 
     { 
      webBrowser1.Stop(); 
      webBrowser1.AllowNavigation = false; 
      return; 
     } 

     isNavigating = true; 
     Console.WriteLine("Navigating to: " + e.Url); 
    } 
+0

這實際上是防止其凍結的非常好的方法。但是,當您停止Web瀏覽器時,似乎還有其他數據需要下載,但未被下載。我想知道是否有辦法忽略導航請求,當它導致「javascript:void((function(){document.open(); document.domain ='costco.com」) –

+0

是的,如果你打電話給web瀏覽器在IsNavigating事件中的Stop()方法中,它會取消導航,但是我認爲在這種情況下,如果你停止了JavaScript調用,它會導致頁面不能正確地加載頁面,我嘗試了幾種停止導航到about:blank或您提到的JavaScript函數;最後我得到的最好運氣就是上面的代碼。 – GojiraDeMonstah

相關問題