Search EggHeadCafe's Job Board
EggHeadCafe Silverlight WPF ASP.NET VB.NET C# Excel SQL Server SharePoint
search
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

WebArticlesForumsFAQs
JavaScript
ASP
ASP.NET
WCF

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

Operating SysArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Lounge
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

  View Other VB.NET Posts   Ask New Question  Ask New Question With Power Editor

WebBrowser DocumentComplete & ReadyState problems
Benjamin Cavallaro posted at Thursday, November 05, 2009 9:09 PM

Im having problems here -

I can use the .navigate method on the webbrowser control to load a page. that works fine. but then when i access the .document property of the webbrowser control, look through the controls within the body, and then click a link on the page, there is no way to figure out when the page has finished loading - because i clicked a link rather than using the .navigate method of the control. so the problem then arises - how can i make my program sit around and wait for the next page to load?

Here are my solutions, none of which are working:
1) tried readystate or documentcomplete event - no luck, they just report everything is loaded bc not using navigate
2) trying to read from webbrowser1.document.body.innerhtml to see if there is anything on the page - this most often throws an error "Object reference not set to an instance of an object."
3) trying to read that, then catch the error and loop back around so the page can finish loading while im catching the error, so there will be an object to reference when i get back

none of this is working. i should mention that sometimes the error isnt thrown at all. sometimes everything loads up quick enough that the object exists to reference when the program is trying to do so. it clicks links on a few pages in a row, so each time i run it i get to a different step before the error is thrown (ie. this time it stopped after the first click, last time it stopped after the third click, etc.)

thoughts?

(code)

Imports System.Windows.Forms

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim jbl as HtmlElement
        Dim GotIt As Boolean
        GotIt = False

        'found first page, now click the first link
        For Each jbl In WebBrowser1.Document.GetElementsByTagName("A")
            If jbl.GetAttribute("href") = "javascript:send_event('SEARCH/SHOW_SEARCH');" Then
                jbl.InvokeMember("click")
                GotIt = True
                Exit For
            End If
        Next

        If GotIt = False Then 'otherwise couldnt find that link
            MsgBox("Please logout, login again and then stay at the homepage")
            GoTo cleaner
        End If
        GotIt = False 'reset this var

        While WebBrowser1.ReadyState <> 4
            Application.DoEvents()
        End While

        While Not WebBrowser1.DocumentTitle = "Test Page"
            Application.DoEvents()
        End While
        'if we make it thru, then page is loaded bc we found title

        Dim gr As String = WebBrowser1.Document.Body.InnerHtml
        Dim pl As Int32 = InStr(gr, "page 1 test")
        If pl <> 0 Then
            GoTo skipahead
        End If

I usually get the error at that point. 
Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0