Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Webbrowser: Wait Until Element is Present

Hey!

Simple question, is there any way I can get a webbrowser to wait until an element is present? I'm not an amazing coder, so here's my best example of what I'm trying to do -

Code
           Do While PageLoaded = False
           Dim OObject As Object
           OObject = WebBrowser1.Document.GetElementById("Element-That-I-Need-Loaded")
           If (OObject Is Nothing) Then
               Pageloaded = False
           Else
               PageLoaded = True
           End If
       Loop

The problem with this is that the element doesn't have an ID, so when I'm trying to do something like click it, I have to do something like this -

Code
           For Each altelm As HtmlElement In WebBrowser1.Document.GetElementsByTagName("SPAN")
           If altelm.GetAttribute("classname").ToString = "Classname-of-Element-I-Need-Loaded" Then
               altelm.Focus()
               altelm.InvokeMember("click")
           End If

I've tried using documentcompleted, but the element loads in after the page, meaning I have to scroll down to load it in. Currently a timer works but if other people use this program with proxies or slower internet, the timer would have to be different.

I apologise if this is worded poorly, I'm new to this :)

Thanks!
If I recall correctly the DocumentCompleted event could fire multiple times. Try checking if the ReadyState property equals WebBrowserReadyState.Complete, and if so then proceed.

Though the WebBrowser component is one of the most unreliable things to make bots/automation tools with. If the task is not too complicated you can try and use the HttpClient, which is much more reliable.
Send the whole code (with the timer included)
Have the timer start once the element is detected in the document completed event. Then have an if statement that detects if the timer is running to keep it from firing multiple times like Ninja said.

You should try out geckofx if you're wanting to do this via web browser. The default control isn't so great lol.
PHP Code

<?php 
Dim _Source 
as string = ""

  
Private Sub WaitForBrowser()

        Do 
Until Not _Source = ""
            
Threading.Thread.Sleep(2000)
        
Loop

    End Sub

Private Sub Web_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles Web.DocumentCompleted
       If e
.Url.ToString = Web.Url.ToString Then
         _Source 
= Web.document.body.innerhtml
       End 
If
   End Sub 
Recently I also faced the same problem but couldn't find solution anywhere and DocumentCompleted event also doesn't always work like you wanted, so I created my own solution this solution works best in multi-threading and also eliminates the problem of waiting for the element for a specific time.

Here's the code I use to wait for webbrowser control to finish initial page loading:

https://pastebin.com/0aEqKtHi
(Jul 31, 2018 04:50 AM)iphone5s Wrote:  Recently I also faced the same problem but couldn't find solution anywhere and DocumentCompleted event also doesn't always work like you wanted, so I created my own solution this solution works best in multi-threading and also eliminates the problem of waiting for the element for a specific time.

Here's the code I use to wait for webbrowser control to finish initial page loading:

https://pastebin.com/0aEqKtHi

Ah nice man, I just went for a Loopcount like this -

Code
            Dim waitcount As Integer = 0

            Do While elementfound = False

                waitcount += 1

                If waitcount = Settings.HuraTextBox1.Text Then
                    GoTo SkipAll
                End If

                For Each altelm As HtmlElement In WebBrowser1.Document.GetElementsByTagName("DIV")
                    If altelm.GetAttribute("classname").ToString = "comment-simplebox-renderer-collapsed comment-simplebox-trigger" Then
                        altelm.Focus()
                        altelm.InvokeMember("click")
                        elementfound = True
                    End If
                Next

                Delay(1)

            Loop


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by roosiinfidel09Aug 19, 2021 07:32 AM 1 172 Aug 19, 2021 07:32 AM
Last Post: john__john
[.NET] by requiredhqMar 15, 2021 07:31 AM 2 176 Mar 15, 2021 07:31 AM
Last Post: The
[Python] by roosiinfidel09May 31, 2020 08:58 AM 9 309 May 31, 2020 08:58 AM
Last Post: synchronizing