VB wait before next action/command

flann

Regular Member
Joined
Jan 19, 2008
Messages
208
Reaction score
34
I have a problem exactly like here:
Code:
http://thebotnet.com/programming/104890-wait-for-2nd-sign-up-page/
Load a website, search for "cookies" and hit the search button, next it will click a link (link is clicked to fast so it failed to click the link on the searchresult page)

Here is my code:
Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        WebBrowser1.Document.GetElementById("query").InnerText = "cookies"
        WebBrowser1.Document.GetElementById("query_button").InvokeMember("click")
        
        [B][U]I NEED A WAIT/DELAY HERE!!!![/U][/B]

        Dim allelements As HtmlElementCollection = WebBrowser1.Document.All

        For Each webpageelement As HtmlElement In allelements

            If webpageelement.InnerText = "Boards" Then
                webpageelement.InvokeMember("click")
            End If
        Next
    End Sub

The solution is this:
Code:
drag a timer component on the form. set its interval to around 1-5 sec. 
after u press the submit button in your first function have it enable the timer. 
in the timer code have it check to see if the page is completed. you can do that by running a foreach loop thru the elements and see if one of the elements matches ur textbox id. 
if the page is completed, disable the timer, and run your code.

I can drag a timer to my form and set the interval, the new code with timer looks like this:
Code:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        WebBrowser1.Document.GetElementById("query").InnerText = "cookies"
        WebBrowser1.Document.GetElementById("query_button").InvokeMember("click")


        Dim allelements As HtmlElementCollection = WebBrowser1.Document.All

        For Each webpageelement As HtmlElement In allelements

            If webpageelement.InnerText = "Boards" Then
                webpageelement.InvokeMember("click")
            End If
        Next
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    End Sub
End Class



Love to know how to start the timer.
 
when you invoke the click function the webrowser leaves the ready state.

Put in a do /loop with a check for the state. also put in a application.doevents() so the thread don't gets totaly locked up
 
when you invoke the click function the webrowser leaves the ready state.

Put in a do /loop with a check for the state. also put in a application.doevents() so the thread don't gets totaly locked up

Thanks, but i don't know how to do this. Is there not a simple 1 line solution? Or can you explain how and where i need to put the loop and application.doevents() ?

(i don't know why you say /loop i use VB/visual_studio and there i don't use the "/")
 
Short code.
Code:
do
 if WebBrowser1.ReadyState = WebBrowserReadyState.Complete then exit do
application.doevents()
loop

not tested it in a long while since i don't work with that object anymore. but should do it.
 
[edit] But definitely try ^^^ that first. I am no programmer and it would be best to go off of the state of the browser in case it's not done loading after X time.

Try this:

Code:
System.Threading.Thread.Sleep(10000)

That is a 10000ms (10s) wait.

Otherwise, you start a timer using
Code:
Timer#.Start()

Ensure the timer is also enabled in the properties window. :)
 
Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        WebBrowser1.Document.GetElementById("query").InnerText = "cookies"
        WebBrowser1.Document.GetElementById("query_button").InvokeMember("click")

        [B]System.Threading.Thread.Sleep(10000)[/B]



        Dim allelements As HtmlElementCollection = WebBrowser1.Document.All

        For Each webpageelement As HtmlElement In allelements

            If webpageelement.InnerText = "Boards" Then
                webpageelement.InvokeMember("click")
            End If
        Next
    End Sub

When i hit the button it is waiting 10sec. before using:
Code:
WebBrowser1.Document.GetElementById("query").InnerText = "cookies"
        WebBrowser1.Document.GetElementById("query_button").InvokeMember("click")

        Dim allelements As HtmlElementCollection = WebBrowser1.Document.All

        For Each webpageelement As HtmlElement In allelements

            If webpageelement.InnerText = "Boards" Then
                webpageelement.InvokeMember("click")

So it is waiting before all the commands not only before:
Code:
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All

        For Each webpageelement As HtmlElement In allelements

I need a wait after the script hitting the search/"query_button" button. So i can use the next page for the next command(s)
 
didn't my code work?
sleep event is never good in singlethreaded environments. it locks everything including the gui.
 
didn't my code work?
sleep event is never good in singlethreaded environments. it locks everything including the gui.

My mistake, i used your code but see no delay/wait.

Now the code is this":

Code:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        WebBrowser1.Document.GetElementById("query").InnerText = "cookies"
        WebBrowser1.Document.GetElementById("query_button").InvokeMember("click")


        Do
            If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then Exit Do
            Application.DoEvents()
        Loop



        Dim allelements As HtmlElementCollection = WebBrowser1.Document.All

        For Each webpageelement As HtmlElement In allelements

            If webpageelement.InnerText = "Boards" Then
                webpageelement.InvokeMember("click")
            End If
        Next
    End Sub
 
Code:
Timer1.Start()
should do the trick.

I wonder why it is not working for me

Code:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        WebBrowser1.Document.GetElementById("query").InnerText = "cookies"
        WebBrowser1.Document.GetElementById("query_button").InvokeMember("click")
        [FONT=Arial Black][COLOR="#FF0000"]Timer1.Start()[/COLOR][/FONT]



        Dim allelements As HtmlElementCollection = WebBrowser1.Document.All

        For Each webpageelement As HtmlElement In allelements

            If webpageelement.InnerText = "Boards" Then
                webpageelement.InvokeMember("click")
            End If
        Next
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    End Sub
End Class

i set the behavior of the ticker: enabled = true (if i set to false i see no changes) am i missing some code or settings?
 
Did you set the timer interval property?
 
Did you set the timer interval property?

yes.

This is just basic stuff, so i wonder why there is no solution.
This problem is the same if i hit a button go to google.com, enter keyword in searchbox and hit enter and next click the first search result. If i do this the way like above, the last click will be on the google.com page and not on the searchresults page.

Or can someone show me for example a simple multiform submitter or google scraper/searcher so i can learn from those scripts/softs.
Big thanks
 
what do you mean you see no delay wait? is it exiting the Do Loop immediately?

My mistake, i used your code but see no delay/wait.

Now the code is this":

Code:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        WebBrowser1.Document.GetElementById("query").InnerText = "cookies"
        WebBrowser1.Document.GetElementById("query_button").InvokeMember("click")


        Do
            If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then Exit Do
            Application.DoEvents()
        Loop



        Dim allelements As HtmlElementCollection = WebBrowser1.Document.All

        For Each webpageelement As HtmlElement In allelements

            If webpageelement.InnerText = "Boards" Then
                webpageelement.InvokeMember("click")
            End If
        Next
    End Sub
 
maybe move the application.doevents() atop of the readystate check. it might be so that the webbrowser event don't have got the CPU time to start.
 
Don't use the WebBrowser control. It may seems easier with it, but it is not.
Using HttpWebRequest/HttpWebResponse or WebClient will save you many headaches.
You can automate almost anything without the browser control.
 
You can try to use sleep(2000)

That's what i always use for my webbrowser when it needs to wait.
 
Back
Top