|
-
Oct 17th, 2011, 01:37 PM
#1
Thread Starter
Addicted Member
Navigate to url and wait.
How do you navigate to a url in a webbrowser and wait for a specific time period like say 30 sec before inputting data into fields in a form? I know how to use WebBrowser1.DocumentCompleted but I don't want to use wait for the complete website to download. There are websites that don't completely download so I don't want to hang up the program.
I tried using Sleep(30000) but that halts the program, it doesn't load the browser for a specific time period.
-
Oct 17th, 2011, 02:16 PM
#2
Hyperactive Member
Re: Navigate to url and wait.
You could use a Timer and set its interval value. Then start the timer when the URL is passed to the WebBrowser control and perform your logic in it's Tick Event.
Although I would first try using the following condition in the DocumentCompleted event:
VB.NET Code:
If Not (WebBrowser1.Document Is Nothing) Then
'Your code here
End If
It is not unusual for the DocumentCompleted event to fire a couple of times when loading a page. The above should make sure that your code doesn't run until the document really has completed.
-
Oct 17th, 2011, 04:24 PM
#3
Thread Starter
Addicted Member
Re: Navigate to url and wait.
Your Timer and set its interval value solution works for one url but how do I loop several urls. When I test the timer, the commands doesn't stop to wait for the timer. For example,
WebBrowser1.Navigate(TextBox1.Text)
Timer1.Start()
WebBrowser1.Navigate(TextBox1.Text)
Timer1.Start()
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If (c = 10) Then
Timer1.Stop()
End If
End Sub
Is there a wait command? How do I start the next url navigation after the timer period ends.
-
Oct 17th, 2011, 07:44 PM
#4
Hyperactive Member
Re: Navigate to url and wait.
For multiple webpages you could navigate to the next site at the end of the Timer's Tick Event.
Here is an example that may help:
VB.NET Code:
Public Class Form1
'A global list that you can fill with links that can be used within the Timers Tick Event
Private Links As List(Of String) = Nothing
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Add links into the List
Links = New List(Of String)(New String() {"http://www.google.com/", "http://www.yahoo.com/", "http://www.vbforums.com/"})
'Navigate to the first link and start the timer
WebBrowser1.Navigate(New Uri(Links.First))
'Saving each Link in the Tag will help keep track of the current URL in the list (just in case the WebBrowser address changes due to a redirect etc).
WebBrowser1.Tag = Links.First
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'On each Tick of the Timer perform your logic
'
'Your code here
'
'Then check if all the links have been navigated
If Links.Last = WebBrowser1.Tag Then
'If the last link was reached then stop the timer
Timer1.Stop()
Links = Nothing
MessageBox.Show("Finished")
Else
'Otherwise navigate to the next link
Dim NextLink As String = Links(Links.IndexOf(WebBrowser1.Tag) + 1)
WebBrowser1.Navigate(New Uri(NextLink))
WebBrowser1.Tag = NextLink
End If
End Sub
End Class
Last edited by JayJayson; Oct 17th, 2011 at 07:47 PM.
-
Oct 18th, 2011, 01:06 PM
#5
Thread Starter
Addicted Member
Re: Navigate to url and wait.
Thank you jay20aiii for sharing your elaborate code. There are some parts of it that is beyond my understanding but I believe I understand the structure.
I don't believe it will do I would like to do which is navigate to a url and wait 30 seconds for the page to load, fill in some input textbox and then navigate to another url and do the same.
From my understanding, your code will not wait 30 seconds to load the page before I add my code. I do not want to use the webbrowser document complete. I'm trying to implement a 30 second pause, delay or wait comment.
-
Oct 18th, 2011, 05:35 PM
#6
Hyperactive Member
Re: Navigate to url and wait.
The code example should do what you need. There are 3 things that would need to be 'tweaked' to make it work for you:
1.
VB.NET Code:
Links = New List(Of String)(New String() {"http://www.google.com/", "http://www.yahoo.com/", "http://www.vbforums.com/"})
Would be populated by your TextBoxes e.g.
VB.NET Code:
Links = New List(Of String)(New String() {TextBox1.Text, TextBox2.Text, TextBox3.Text})
2. In the Designer after adding the Timer control to your form, you would need to set the Interval property to the relevant number of milliseconds. This would control how long the app waits before the Tick event runs. This is how you would make your code run 30 seconds after loading the URL.
3. The code you want to run after 30 seconds would go in the Tick Event at the point I commented.
The only other way I can think of to create a time delay, without freezing the WebBrowser control, would be to use:
... within a loop. The problem with that is it uses a lot of CPU. You might be able to combine the following in a loop to get the effect you need without eating too much CPU:
VB.NET Code:
Application.DoEvents
System.Threading.Thread.Sleep(200)
Last edited by JayJayson; Oct 18th, 2011 at 05:38 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|