2016-02-08 46 views
2

我想做一個2步的源下載過程。所以我有一個像等待,直到網頁瀏覽器加載第一頁Visual Basic Net

WebBrowser1.Navigate("http://www.first-site.com") 

'HERE code for downloading and saving the source code of the site into a variable 
'Directly after having downloaded the source, open the second site and do the same 

WebBrowser1.Navigate("http://www.first-site.com/sub-site") 
'Download the Source code into one string variable 

的問題是,我的網頁瀏覽器會後直接相互加載兩個網站非常非常快,不會讓我下載來源爲這兩個網站。

我嘗試了一切,我用google搜索「等待瀏覽器加載一個網站」等等,以及與while循環有關的所有事情,以查看瀏覽器是否加載了網站會導致我的程序崩潰。

怎麼辦?

+0

如果你只是想下載源代碼,你可能會更好過了['HttpWebRequest'](https://msdn.microsoft.com/de-de /library/system.net.httpwebrequest(v=vs.110).aspx)。 –

+0

我的答案解決了你的問題嗎?如果是這樣,你會相應地標記它。如果沒有,請告訴我,我會盡力幫助。 – InteXX

回答

0

你會想要使用DocumentCompleted事件,如記錄here

在您的事件處理程序方法中,請致電WebBrowser1.Navigate()導航到第二個URL。檢查e.Url以查看哪個URL正在完成,因此您不會以循環方式結束。

從文檔:

Private Sub PrintHelpPage() 
    ' Create a WebBrowser instance. 
    Dim webBrowserForPrinting As New WebBrowser() 

    ' Add an event handler that prints the document after it loads. 
    AddHandler webBrowserForPrinting.DocumentCompleted, New _ 
     WebBrowserDocumentCompletedEventHandler(AddressOf PrintDocument) 

    ' Set the Url property to load the document. 
    webBrowserForPrinting.Url = New Uri("\\myshare\help.html") 
End Sub 

Private Sub PrintDocument(Sender As Object, e As WebBrowserDocumentCompletedEventArgs) 
    Dim webBrowserForPrinting As WebBrowser = CType(sender, WebBrowser) 

    ' Print the document now that it is fully loaded. 
    webBrowserForPrinting.Print() 
    MessageBox.Show("print") 

    ' Dispose the WebBrowser now that the task is complete. 
    webBrowserForPrinting.Dispose() 
End Sub