VS 2010 How to tell web browser to wait for new page/specific url to load before continuing??-VBForums
Results 1 to 16 of 16

Thread: How to tell web browser to wait for new page/specific url to load before continuing??

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Location
    United Kingdom
    Posts
    20

    Exclamation How to tell web browser to wait for new page/specific url to load before continuing??

    Hey everyone I know I am new here and I am new to VB but I am well into making my Twitter account maker which just started out as a bit of fun and then into a project to speed up making verified accounts for my marketing and now I am hooked into improving as it is turning into a very good program I am just stuck with one thing which I can see being a very simple solution but I have search forum after forum and tonnes of Google searches all worded different to solve my my.

    It really is simple at present I use different buttons to automate different parts/pages of the account creation I am now setting it up so I click say for example a "go" button and it fills in the first page (and heres where I'm stuck waits for a specific url or even just a new url to load before continuing with the next bit of code????

    Please how do I make it wait it seems like such a simple thing to do yet I cannot find any code online that works I have figured out everything along the lines and have even started to get the general hang of understanding the code compared to when I started it all seemed so unlogical lol.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,578

    Re: How to tell web browser to wait for new page/specific url to load before continui

    The reason you can't find a way is because there isn't one. You don't "make it wait". You call Navigate and then you let it do its thing. When it finishes loading the page, it will tell you. It does so by raising its DocumentCompleted event. You handle the event and, when it's raised, you do whatever it is that you want to do when the page has finished loading. Just note that, if it contains sub-documents, a page may prompt more than one DocumentCompleted event. You should start by reading the documentation for the event and then search for examples on the web to learn how to use it properly.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Location
    United Kingdom
    Posts
    20

    Re: How to tell web browser to wait for new page/specific url to load before continui

    I am still new to this but I have stumbled across DocumentCompleted while searching for ways to solve my problem I also know my questions sound very stupid to some I can assure you I do search first in fact I search everywhere before asking questions and have already learnt so much just from Google searches, forums and YouTube Video's.

    I do appreciate your reply to my question as I have been getting more and more frustrated with this issue and not being able to find a solution was even more annoying so thanks. I don't suppose you can point me in the direction of any forum posts, guides or anything on using DocumentCompleted and handling events etc.

    I do not mind reading or even having to do research just to understand what a post/guide/thread/example is going on about so please do not hold back if you feel I would not understand I have of course started reading up on this as we speak but just thought you might know of a post etc which would help if not no worries thanks for your help above.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,578

    Re: How to tell web browser to wait for new page/specific url to load before continui

    There's not much to it really. If you were thinking that you wanted to do this:
    vb.net Code:
    1. Private Sub Button1_Click(...) Handles Button1.Click
    2.     WebBrowser1.Navigate("some URL")
    3.     WaitForDocumentToLoad()
    4.     DoSomething()
    5. End Sub
    then you do this instead:
    vb.net Code:
    1. Private Sub Button1_Click(...) Handles Button1.Click
    2.     WebBrowser1.Navigate("some URL")
    3. End Sub
    4.  
    5. Private Sub WebBrowser1_DocumentCompleted(...) Handles WebBrowser1.DocumentCompleted
    6.     If Not WebBrowser1.IsBusy Then
    7.         DoSomething()
    8.     End If
    9. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Location
    United Kingdom
    Posts
    20

    Re: How to tell web browser to wait for new page/specific url to load before continui

    Quote Originally Posted by jmcilhinney View Post
    There's not much to it really. If you were thinking that you wanted to do this:
    vb.net Code:
    1. Private Sub Button1_Click(...) Handles Button1.Click
    2.     WebBrowser1.Navigate("some URL")
    3.     WaitForDocumentToLoad()
    4.     DoSomething()
    5. End Sub
    then you do this instead:
    vb.net Code:
    1. Private Sub Button1_Click(...) Handles Button1.Click
    2.     WebBrowser1.Navigate("some URL")
    3. End Sub
    4.  
    5. Private Sub WebBrowser1_DocumentCompleted(...) Handles WebBrowser1.DocumentCompleted
    6.     If Not WebBrowser1.IsBusy Then
    7.         DoSomething()
    8.     End If
    9. End Sub
    Thank you for taking the time to reply to my thread I will try this but I just wanted to explain what I am trying to do and ask if the above code is what I need to use.

    I will be clicking a button which will fill all the text forms from text box's then it clicks submit (This is the part I need it to wait/pause/etc here) and then fills in the next pages text box and click submit again.

    I am going to try to implement the code to my project and your right it does look very simple right from when I first encountered this problem I was surprised I was able to complete other tasks but was totally stuck with this one.

    If I am unable to get this working I will post an example of my code but I am usually pretty good at working things like these out on my own well with good old Google's help but I am very grateful for all the help I have received so far I know I may not use the correct terminology for various processes etc but I am learning VB 2010 slowly but surely lol

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Location
    United Kingdom
    Posts
    20

    Re: How to tell web browser to wait for new page/specific url to load before continui

    I may be totally off base but here goes any ways

    I have a web browser which is set (in properties) to go to Twitter's Mobile Sign Up Page (which recently changed to now be over 2 pages instead of just one.

    So my program fills in the first page clicks the button and this is where my problem is because I have used the document complete but this just trys to run the code straight away.

    Either I am missing some way of specifying when to run this piece of code or I am on the totally wrong tracks.

    Believe me I have spent hours reading other posts and countless searches on Google and found nothing on the exact same lines as my problem apart from I stumbled across what appears to be a similar to document complete but for navigation is this what I should be looking at????

    In simple words I am completing a sign up process and need to halt the program from continuing until it has clicked continue and loads the next page???

    Even just examples of code doing this should be enough for me to figure out the rest but please if anyone knows what I am trying to achieve and knows how I could really do with your help as I am so determined to figure this out

  7. #7
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    4,948

    Re: How to tell web browser to wait for new page/specific url to load before continui

    vb Code:
    1. ' Do something
    2.  
    3. While Me.WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
    4.    Application.DoEvents()
    5. End While
    6.  
    7. ' Do something

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Location
    United Kingdom
    Posts
    20

    Re: How to tell web browser to wait for new page/specific url to load before continui

    This is the code for the button which gets clicked to start the account creation process

    vb Code:
    1. WebBrowser1.Document.GetElementById("oauth_signup_client[fullname]").SetAttribute("value", fullnametb.Text)
    2.         WebBrowser1.Document.GetElementById("oauth_signup_client[email]").SetAttribute("value", emailtb.Text)
    3.         WebBrowser1.Document.GetElementById("oauth_signup_client[password]").SetAttribute("value", passwordtb.Text)
    4.         WebBrowser1.Document.GetElementById("captcha_response_field").SetAttribute("value", captchatb.Text)
    5.         If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
    6.             Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
    7.  
    8.             For Each webpageelement As HtmlElement In allelements
    9.  
    10.                 If webpageelement.GetAttribute("type") = "submit" Then
    11.  
    12.                     webpageelement.InvokeMember("click")
    13.  
    14.                 End If
    15.  
    16.             Next
    17.         End If

    I am sorry being such a noob I know how annoying it can be on forums when people appear to be asking simple questions but if I wanted the above code to wait for the new page to load (invoke member click will click continue and thus load new page how do I then get it to carry out the below after waiting for the new page to load instead of what it does now trying to do it all at once

    vb Code:
    1. WebBrowser1.Document.GetElementById("settings[screen_name]").SetAttribute("value", usernametb.Text)

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,578

    Re: How to tell web browser to wait for new page/specific url to load before continui

    Quote Originally Posted by ident View Post
    vb Code:
    1. ' Do something
    2.  
    3. While Me.WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
    4.    Application.DoEvents()
    5. End While
    6.  
    7. ' Do something
    I'm afraid that that is terrible advice. Never, ever call Application.DoEvents in a loop under any circumstances.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Location
    United Kingdom
    Posts
    20

    Re: How to tell web browser to wait for new page/specific url to load before continui

    Guess I wont be trying that then lol :P I don't think I have ever got this stressed trying to figure something like this out in all honesty I thought it would be easy to implement a way of waiting for a specific url to load or even just a new page before carrying on

    I think I could put a book together with all the forum posts I found asking the same thing as me just worded differently lol

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,479

    Re: How to tell web browser to wait for new page/specific url to load before continui

    So you didn't understand post #4 at all - right?

    Do you get what an EVENT is and that the EVENT fires and runs a function when "something" happens?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Location
    United Kingdom
    Posts
    20

    Re: How to tell web browser to wait for new page/specific url to load before continui

    Yea I got post 4 and even played around with it to make sure I understood it fully as I was using document completed before without realising.

    How do I differentiate from document completed from webbrowser1's first and second page, if that didn't make sense then this might I have filled out the text boxes on page one and then clicked continue with invoke member now how do I call/wait for document completed on the new page instead of the old one

    I am getting the feeling due to my lack of terminology and stress lol many do not understand what I am trying to say.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Location
    United Kingdom
    Posts
    20

    Re: How to tell web browser to wait for new page/specific url to load before continui

    I've done it OMG at last its only taken me like all day to do lol

    I ended up using
    vb Code:
    1. Call WaitForPageLoad()

    vb Code:
    1. Private Property pageready As Boolean = False
    2.  
    3. #Region "Page Loading Functions"
    4.     Private Sub WaitForPageLoad()
    5.         AddHandler whatbrowser.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    6.         While Not pageready
    7.             Application.DoEvents()
    8.         End While
    9.         pageready = False
    10.     End Sub
    11.  
    12.     Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    13.         If whatbrowser.ReadyState = WebBrowserReadyState.Complete Then
    14.             pageready = True
    15.             RemoveHandler whatbrowser.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    16.         End If
    17.     End Sub
    18.  
    19. #End Region

    Is there any reason why using this method is any different to placing
    vb Code:
    1. If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
    2.  
    3. DO WHAT I WANTED YOU TO DO IN THE FIRST PLACE
    where I placed WaitForPageLoad???????

    On another note I am sorry for my noobish terminology and what probably appears to be some one just messing around I can assure you I do always try to teach myself and when I get motivated I figure things out using trial and error just as in this case and although I hit a lot of problems and guesswork lol I learnt a lot from today. Some may find my learning methods some what different but it usually all works out ok I hate being a noob lol I kinda got used to being some one in the know on SEO forums etc and now I'm back to the bottom lool :P

  14. #14
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,246

    Re: How to tell web browser to wait for new page/specific url to load before continui

    FWIW, This is what I'm using in a app I'm currently working on for a specific site, so far its the only code that has worked for me, and I've tried a lot of things, including post #4.

    Code:
    Private Sub WebBrowser1_DocumentCompleted(....
    
        If (e.Url.AbsolutePath = Me.WebBrowser1.Url.AbsolutePath) Then
           ' I call my code here
        End If
    
    End Sub

  15. #15
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    4,948

    Re: How to tell web browser to wait for new page/specific url to load before continui

    Quote Originally Posted by jmcilhinney View Post
    I'm afraid that that is terrible advice. Never, ever call Application.DoEvents in a loop under any circumstances.
    really? Thanks for the advice. Can i ask why. I did wonder why you never suggested it.

    sam

  16. #16
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,479

    Re: How to tell web browser to wait for new page/specific url to load before continui

    Please look at your task manager cpu usage - before you run this loop.

    And then after as well - running your app - running in the loop

    ...but stopping the reply from ever happening!!!

    change your code to this

    Code:
        Private Sub WaitForPageLoad()
            AddHandler whatbrowser.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)        
            While 1=1 ' Not pageready 
                          ' fake the loop into running forever         
                Application.DoEvents()        
            End While        
            pageready = False    
        End Sub
    What does the cpu do?

    If it spikes - well - you are not allowed to do that. Even for 10 ms

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.