none
webbrowser documentcompleted event looping ? RRS feed

  • Question

  •    Try
                Dim wb As New WebBrowser
                AddHandler wb.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf webclientst.WeBrowserComplete)
    
                wb.Navigate(url)
                MsgBox("navigating")
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                logError(ex.Message)
            End Try


        Public Shared Sub WeBrowserComplete(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
            MsgBox("completed")
        End Sub
    

    These are my scripts, i got navigating messagebox once and completed message box is looping. The thing I want to do is Thread.sleep(20000) after loading it, and then do a invokeclick and then it will start a download in which I want the download to be cancelled and link to be stored in strdl as string.

    The link gets genrated only when the button is clicked so I guess its good. Now i need to know how to first sleep and then click on it and get to the download event after that I will be able to go ...

    Tuesday, October 14, 2014 3:22 PM

All replies

  • First I wouldn't create a new WebBrowser unless you can dispose of it in the document completed event. Otherwise you are constantly creating WebBrowser objects and not disposing of them which over time will increase memory size as each non-disposed of object is added to memory. By disposing of the object when done with it the garbage collector will remove it from memory.

    Also if you dispose of it in the document completed event I would remove the handler that was previously added to it first although that may not matter. Or perhaps a crash may occur if it is disposed of prior to the handler being removed.

    I know you can't add a handler again to a control that already has that handler added to it.

    Instead of creating a new webbrowser object constantly this code reuses one created at Form Load so only one WebBrowser object is ever created.

    As far as I know what you are experiencing with the Document Completed event is caused by accessing a WebSite that has IFrames (An IFrame (Inline Frame) is an HTML document embedded inside another HTML document on a website. The IFrame HTML element is often used to insert content from another source, such as an advertisement, into a Web page) embedded in the HTML document the WebBrowser control downloaded. And those cause it to add by downloading that information to be inserted into the already downloaded HTML document or something. Which causes the Document Completed event to fire more than once.

    Anyhow you can detect if the WebBrowser control is busy in the Document Completed event. If it is then don't do anything. If it's not then do something.

    This code detects that. Apparently YouTube uses IFrames as you can see in the images below that the Document Completed event fired three times while the WebBrowser was downloading an HTML document from YouTube. But the IsBusy if statement only fired once, after the WebBrowser was done and no longer connected I suppose.

    Option Strict On
    
    Public Class Form1
    
        WithEvents WB As New WebBrowser
    
        Dim DocCompEventCounter As Integer = 0
        Dim IsBusyFalseCounter As Integer = 0
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            Label1.Text = "Waiting"
            Label2.Text = "Waiting"
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            RichTextBox1.Clear()
            Label1.Text = "Waiting"
            Label2.Text = "Waiting"
            If TextBox1.Text <> "" Then
                Try
                    DocCompEventCounter = 0
                    IsBusyFalseCounter = 0
                    With WB
                        .ScriptErrorsSuppressed = True
                        .AllowNavigation = True
                        .Navigate(TextBox1.Text)
                    End With
                    AddHandler WB.DocumentCompleted, AddressOf WB_DocumentCompleted
                Catch ex As Exception
                    Label1.Text = ex.Message
                End Try
            End If
        End Sub
    
        Private Sub WB_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
            If WB.IsBusy = False Then
                IsBusyFalseCounter += 1
                Label2.Text = "The IsBusy = False If statement fired " & IsBusyFalseCounter.ToString & " times."
                RichTextBox1.Text = WB.DocumentText
                RemoveHandler WB.DocumentCompleted, AddressOf WB_DocumentCompleted
            End If
            DocCompEventCounter += 1
            Label1.Text = "The DocumentCompleted event fired " & DocCompEventCounter.ToString & " times."
        End Sub
    
    End Class
    


    La vida loca

    Tuesday, October 14, 2014 8:09 PM
  • First I wouldn't create a new WebBrowser unless you can dispose of it in the document completed event. Otherwise you are constantly creating WebBrowser objects and not disposing of them which over time will increase memory size as each non-disposed of object is added to memory. By disposing of the object when done with it the garbage collector will remove it from memory.

    Also if you dispose of it in the document completed event I would remove the handler that was previously added to it first although that may not matter. Or perhaps a crash may occur if it is disposed of prior to the handler being removed.

    I know you can't add a handler again to a control that already has that handler added to it.

    Instead of creating a new webbrowser object constantly this code reuses one created at Form Load so only one WebBrowser object is ever created.

    As far as I know what you are experiencing with the Document Completed event is caused by accessing a WebSite that has IFrames (An IFrame (Inline Frame) is an HTML document embedded inside another HTML document on a website. The IFrame HTML element is often used to insert content from another source, such as an advertisement, into a Web page) embedded in the HTML document the WebBrowser control downloaded. And those cause it to add by downloading that information to be inserted into the already downloaded HTML document or something. Which causes the Document Completed event to fire more than once.

    Anyhow you can detect if the WebBrowser control is busy in the Document Completed event. If it is then don't do anything. If it's not then do something.

    This code detects that. Apparently YouTube uses IFrames as you can see in the images below that the Document Completed event fired three times while the WebBrowser was downloading an HTML document from YouTube. But the IsBusy if statement only fired once, after the WebBrowser was done and no longer connected I suppose.

    Option Strict On
    
    Public Class Form1
    
        WithEvents WB As New WebBrowser
    
        Dim DocCompEventCounter As Integer = 0
        Dim IsBusyFalseCounter As Integer = 0
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            Label1.Text = "Waiting"
            Label2.Text = "Waiting"
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            RichTextBox1.Clear()
            Label1.Text = "Waiting"
            Label2.Text = "Waiting"
            If TextBox1.Text <> "" Then
                Try
                    DocCompEventCounter = 0
                    IsBusyFalseCounter = 0
                    With WB
                        .ScriptErrorsSuppressed = True
                        .AllowNavigation = True
                        .Navigate(TextBox1.Text)
                    End With
                    AddHandler WB.DocumentCompleted, AddressOf WB_DocumentCompleted
                Catch ex As Exception
                    Label1.Text = ex.Message
                End Try
            End If
        End Sub
    
        Private Sub WB_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
            If WB.IsBusy = False Then
                IsBusyFalseCounter += 1
                Label2.Text = "The IsBusy = False If statement fired " & IsBusyFalseCounter.ToString & " times."
                RichTextBox1.Text = WB.DocumentText
                RemoveHandler WB.DocumentCompleted, AddressOf WB_DocumentCompleted
            End If
            DocCompEventCounter += 1
            Label1.Text = "The DocumentCompleted event fired " & DocCompEventCounter.ToString & " times."
        End Sub
    
    End Class


    La vida loca

    Thanks. Can I remove the handler and then add it again ?

    Yes I will be disposing of all of it once I got strdl. Will over 20-40 webbrowsers at a time affect memory ?

    Wednesday, October 15, 2014 2:08 AM
  • I want to click this,

    <div id="dlblock" class="dlb" style="display: block;">
    				
    				<input type="hidden" name="down_direct" value="1"><input type="submit" id="btn_download" class="redbtn" value="DOWNLOAD (56.5 MB)">
    			</div>


    and then get a download started, I have added,

      AddHandler wb.Navigating, New WebBrowserNavigatingEventHandler(AddressOf wb_Navigator)
            AddHandler wb.FileDownload, New eventhandler(AddressOf wb_FileDownload)

    and did e.cancel and printed on console but nothing was printed. Is there a way ?

    The button will appear after 20 seconds so i used thread.sleep(20000) before clicking. I managed to wait till the document complete with bool and setting it byref.

    But when thread sleeps will the webbrowser continue to countdown ?

    Edit: 

     AddHandler wb.Navigating, New WebBrowserNavigatingEventHandler(AddressOf wb_Navigator)
            AddHandler wb.FileDownload, New eventhandler(AddressOf wb_FileDownload)
    
            Dim elc As HtmlElementCollection = wb.Document.GetElementsByTagName("input")
            For Each el As HtmlElement In elc
    
                If (el.GetAttribute("type").Equals("submit")) Then
                    el.InvokeMember("Click")
                    Console.WriteLine("clicked")
                End If
            Next

    I get clicked on console but nothing else. Can you say what I am doing wrong ? I want the download link stored in strdl.

    • Edited by Sugavanas Wednesday, October 15, 2014 2:40 AM
    Wednesday, October 15, 2014 2:35 AM
  • I have no idea why you are using thread sleep and what you mean by the button will appear after 20 seconds.

    Perhaps you could provide a link to the site in question so responders could look at the site to see what you mean by the button will appear after 20 seconds.

    I would imagine that a button being displayed by some website after a certain time period will only appear whenever it is displayed in your browser. And that can occur after whatever time you think it should occur depending on the number of requests the website is dealing with and congestion on the net between you and the website.

    But your request is cryptic and not explained very well so I don't know what you mean.

    The code I provided detects when the webbrowser is no longer busy. I thought that's what you were looking for.


    La vida loca

    Wednesday, October 15, 2014 2:58 AM
  • I have no idea why you are using thread sleep and what you mean by the button will appear after 20 seconds.

    Perhaps you could provide a link to the site in question so responders could look at the site to see what you mean by the button will appear after 20 seconds.

    I would imagine that a button being displayed by some website after a certain time period will only appear whenever it is displayed in your browser. And that can occur after whatever time you think it should occur depending on the number of requests the website is dealing with and congestion on the net between you and the website.

    But your request is cryptic and not explained very well so I don't know what you mean.

    The code I provided detects when the webbrowser is no longer busy. I thought that's what you were looking for.


    La vida loca

    here A example link,

    http://rockdizfile.com/piftk0ebsqy2

    and this : 

    http://rockdizfile.com/kz1v3oacwlso

    one doesn't has wait time one has but i better wait for both. The wait time is 15 secs but I used 20 secs to be more save :P

    Wednesday, October 15, 2014 4:51 AM