Results 1 to 3 of 3

Thread: looping to wait until web element exists, but adding timer to time out

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    looping to wait until web element exists, but adding timer to time out

    my code is as follows:

    Code:
     
    
    Private Sub ReqCheck()
            Dim ele As Object = htmldoc.getElementById("requirement0")
    
            wait(500)
    
            Do Until Not ele Is Nothing
    
                htmldoc = AxWebBrowser1.Document.frames("MainFrame").document
                ele = htmldoc.getElementById("requirement0")
                wait(500)
          Loop
    
            htmldoc = AxWebBrowser1.Document
            MessageBox.Show(ele.innertext) 'testing to make sure it got the element
    
        End Sub
    
        Private Sub wait(ByVal interval As Integer)
    
            Dim stopW As New Stopwatch
            stopW.Start()
            Do While stopW.ElapsedMilliseconds < interval
                ' Allows your UI to remain responsive
                Application.DoEvents()
            Loop
            stopW.Stop()
        End Sub
    I am using this to verify that an element exists in the htmldoc collection (it will always exist on the page, perhaps my loop is a bit redundant? new to .net). What I need to figure out is how to time this loop out after X amount of time passes, just in case the server is down, or is going really slow, a message box can be popped up and the sub exited. I looked around, but I am unsure.. perhaps someone could school me a bit
    Last edited by jayinthe813; Oct 26th, 2012 at 12:18 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: looping to wait until web element exists, but adding timer to time out

    This question just keeps getting asked over and over. I think that I've answered it myself about 4 times in the last couple of weeks. You do not use a polling loop to determine when a page has loaded in a web browser control. You handle the appropriate event. I haven't used an ActiveX web browser control for a long time so I don't actually recall what the correct event is. You should use the .NET WebBrowser control unless you have a very good reason not to, in which case the event is DocumentCompleted. Given that you can still access the internal ActiveX control via the ActiveXInstance property of a .NET WebBrowser control, there just isn't a good reason not to use it. You can then use the managed interface as much as possible and the COM interface only when you need to.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: looping to wait until web element exists, but adding timer to time out

    the overall idea for what im doing is to enter data into a textbox and click a button, and ultimately parse the pages (using the name property, there is no ID)

    Code:
     htmldoc = AxWebBrowser1.Document.frames("top").document 'set the top frame to be searched for
    
            setval = htmldoc.getElementsByName("caseNum").item(0) 'grab the input name for contracts
    
            setval.value = TextBox1.Text 'set the textbox value into the web form input
            TextBox1.Clear()
    
            htmldoc.getElementsByName("GetCase").item(0).click() 'click the submit butt
    after it clicks, the onclick event loads some kind of intermediary page while it looks up the case, the sends you to the final page with the case information ( i assume the web page is locking the case to a session user in the intermediary web page). I tested it with messageboxes and i ended up getting 6 or so messagebox popups when using document complete. Im not sure how to parse .getelementbyname either with the .net browser, can you enlighten me a bit? Would you also not use mshtml with that and rely on .net entirely? I cant get compatability with the two, and even setting htmldoc = webbrowser.document.domdocument wont run the getelement methods correctly. how would i go about using the activex browser from the .net browser?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width