1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

VB wait before next action/command

Discussion in 'Visual Basic .NET' started by flann, May 7, 2012.

  1. flann

    flann Regular Member

    Joined:
    Jan 19, 2008
    Messages:
    208
    Likes Received:
    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.
     
  2. HatIsBlack

    HatIsBlack Regular Member

    Joined:
    Sep 17, 2010
    Messages:
    265
    Likes Received:
    187
    Location:
    Where i belong
    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
     
  3. flann

    flann Regular Member

    Joined:
    Jan 19, 2008
    Messages:
    208
    Likes Received:
    34
    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 "/")
     
  4. HatIsBlack

    HatIsBlack Regular Member

    Joined:
    Sep 17, 2010
    Messages:
    265
    Likes Received:
    187
    Location:
    Where i belong
    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.
     
  5. meatro

    meatro BANNED BANNED

    Joined:
    Nov 21, 2009
    Messages:
    568
    Likes Received:
    997
    [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. :)
     
  6. flann

    flann Regular Member

    Joined:
    Jan 19, 2008
    Messages:
    208
    Likes Received:
    34
    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)
     
  7. HatIsBlack

    HatIsBlack Regular Member

    Joined:
    Sep 17, 2010
    Messages:
    265
    Likes Received:
    187
    Location:
    Where i belong
    didn't my code work?
    sleep event is never good in singlethreaded environments. it locks everything including the gui.
     
  8. flann

    flann Regular Member

    Joined:
    Jan 19, 2008
    Messages:
    208
    Likes Received:
    34
    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
     
  9. openaidbh

    openaidbh BANNED BANNED

    Joined:
    Mar 3, 2012
    Messages:
    328
    Likes Received:
    320
    Code:
    Timer1.Start()
    should do the trick.
     
  10. flann

    flann Regular Member

    Joined:
    Jan 19, 2008
    Messages:
    208
    Likes Received:
    34
    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?
     
  11. kill4

    kill4 Regular Member

    Joined:
    May 11, 2009
    Messages:
    247
    Likes Received:
    42
    Location:
    Texas
    Did you set the timer interval property?
     
  12. flann

    flann Regular Member

    Joined:
    Jan 19, 2008
    Messages:
    208
    Likes Received:
    34
    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
     
  13. flymethod

    flymethod Regular Member

    Joined:
    Oct 27, 2010
    Messages:
    214
    Likes Received:
    58
    what do you mean you see no delay wait? is it exiting the Do Loop immediately?

     
  14. HatIsBlack

    HatIsBlack Regular Member

    Joined:
    Sep 17, 2010
    Messages:
    265
    Likes Received:
    187
    Location:
    Where i belong
    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.
     
  15. theMagicNumber

    theMagicNumber Regular Member

    Joined:
    May 13, 2010
    Messages:
    345
    Likes Received:
    195
    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.
     
  16. Soufian

    Soufian Newbie

    Joined:
    Feb 8, 2012
    Messages:
    14
    Likes Received:
    0
    You can try to use sleep(2000)

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