等到Web浏览器加载第一页Visual Basic Net

时间:2016-02-08 10:50:04

标签: vb.net web

我想做一个2步源下载过程。所以我喜欢

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

问题是我的网页浏览器会非常快地直接加载两个网站,而不是让我下载两个网站的资源。

我尝试了所有内容,我用谷歌搜索“等待浏览器加载一个网站”等等,以及与while循环有关的所有内容,看看浏览器是否加载了一个网站,导致程序崩溃。

怎么办?

1 个答案:

答案 0 :(得分:0)

您需要使用DocumentCompleted事件,如文档here所述。

在您的事件处理程序方法中,调用WebBrowser1.Navigate()以导航到第二个URL。检查e.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