Hack Forums
[.NET] Webbrowser: Wait Until Element is Present - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Webbrowser: Wait Until Element is Present (/showthread.php?tid=5819988)



Webbrowser: Wait Until Element is Present - ThatAlbinoFrog - Apr 3, 2018

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!


RE: Webbrowser: Wait Until Element is Present - Ninja - Apr 3, 2018

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.


RE: Webbrowser: Wait Until Element is Present - SIM-2362662 - Apr 3, 2018

Send the whole code (with the timer included)


RE: Webbrowser: Wait Until Element is Present - Apparatus - Apr 4, 2018

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.


RE: Webbrowser: Wait Until Element is Present - Mike Tyler - Jul 28, 2018

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 



RE: Webbrowser: Wait Until Element is Present - iphone5s - Jul 31, 2018

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


RE: Webbrowser: Wait Until Element is Present - ThatAlbinoFrog - Jul 31, 2018

(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