Sebastian Ahlen February 2016

Wait until web browser has loaded first page Visual Basic Net

I want to do a 2 step Source-Download-Process. So I have like

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

The problem is that my web browser will be loading two sites directly after each other very very quickly, not enabling me to download sources for both sites.

I tried everything, I googled "wait for browser to load one site" etc. and everything that has to do with while loops to see whether a browser has loaded a site it crashes my program.

What to do?

Answers


InteXX February 2016

You'll want to use the DocumentCompleted event, as documented here.

Within your event handler method, call WebBrowser1.Navigate() to navigate to the second URL. Check e.Url to see which URL is being completed so you don't end up in a loop.

From the documentation:

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

Post Status

Asked in February 2016
Viewed 1,474 times
Voted 8
Answered 1 times

Search




Leave an answer