1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Issue With WebBrowser & Clicking Buttons

Discussion in 'Visual Basic .NET' started by gimme4free, Apr 18, 2013.

  1. gimme4free

    gimme4free Executive VIP Jr. VIP Premium Member

    Joined:
    Oct 22, 2008
    Messages:
    1,881
    Likes Received:
    1,932
    For some reason I am having some issues clicking the Like / DisLike button on comments on YouTube & wondered if anyone can spot the issue that I have come across.

    Everything runs ok UP TO the invoke("click") which simply doesn't seem to fire. I'm used to HTTPWebRequest but never bothered getting into WebBrowser too much so have probably made a mistake here somewhere!

    Code:
    Code:
    Private Sub WebBrowserLikeComment(ByRef newBrowser As WebBrowser, ByVal VideoID As String, ByVal CommentID As String, Optional ByVal Dislike As Boolean = False)
            ' Set Variables
            Dim FoundComment As Boolean = False
            Dim CurTime As Integer = 0
            For i As Integer = 1 To 10
                ' Reset Timeout
                CurTime = 0
                ' Load Comments Page
                newBrowser.Navigate("http://www.youtube.com/all_comments?v=" & VideoID & "&page=" & i)
                While (True)
                    Application.DoEvents()
                    If (newBrowser.ReadyState = WebBrowserReadyState.Complete) Then
                        Thread.Sleep(100)
                        If newBrowser.DocumentText.Contains(CommentID) Then
                            FoundComment = True
                            GoTo ContinueProcess001
                        Else
                            MessageBox.Show("Next Page")
                            Continue For
                        End If
                    End If
                    Thread.Sleep(100)
                    CurTime += 1
                    If CurTime >= (My.Settings.MaxTimeout * 10) Then Throw New Exception("Loading YouTube Comments Page Timed Out")
                End While
            Next
    ContinueProcess001:
            ' Confirm Found Comment
            If FoundComment = False Then Throw New Exception("Failed To Find Comment: " & CommentID & " For Video ID: " & VideoID)
            ' Scrape All Comments
            Dim HTMLCollection As HtmlElementCollection
            Try
                HTMLCollection = newBrowser.Document.GetElementById("all-comments").GetElementsByTagName("li")
            Catch ex As Exception
                Throw New Exception("Failed To Parse Comment HTML Elements For Video: " & VideoID)
            End Try
            ' Loop Elements
            Dim Voted As Boolean = False
            For Each TmpElement As HtmlElement In HTMLCollection
                If TmpElement.InnerHtml = Nothing Then Continue For
                If Voted = True Then Exit For
                ' Check For Comment
                If TmpElement.InnerHtml.Contains(CommentID) Then
                    ' MessageBox.Show("Found Element")
                    ' Reset Elements
                    Dim htmlColpom As HtmlElementCollection = TmpElement.Children(2).Children(2).GetElementsByTagName("span")
                    ' Loop Elements
                    For Each TmpButtonElement As HtmlElement In htmlColpom
                        If TmpButtonElement.InnerHtml = Nothing Then Continue For
                        If Dislike = False AndAlso TmpButtonElement.InnerHtml.Contains("Vote Up") Then
                            MessageBox.Show(TmpButtonElement.InnerHtml)
                            ' Click Vote Button
                            TmpButtonElement.Focus()
                            Thread.Sleep(100)
                            TmpButtonElement.InvokeMember("click")
                            ' SendKeys.Send("{ENTER}")
                            Application.DoEvents()
                            Voted = True
                            Exit For
                        ElseIf Dislike = True AndAlso TmpButtonElement.InnerHtml.Contains("Vote Down") Then
                            MessageBox.Show(TmpButtonElement.InnerHtml)
                            ' Click Vote Button
                            TmpButtonElement.Focus()
                            Thread.Sleep(100)
                            TmpButtonElement.InvokeMember("click")
                            ' SendKeys.Send("{ENTER}")
                            Application.DoEvents()
                            Voted = True
                            Exit For
                        End If
                    Next
                End If
            Next
            If Voted = False Then Throw New Exception("Unable To Find Vote Up Button")
        End Sub
    
     
  2. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,148
    I don't use WebBrowser and my strategy is to interact via injecting JS, so I 'll answer in that fashion - if you don't get anywhere with WebBrower, this may come handy.

    I just tested it in FF and it works

    Code:
    
    function fireClick(node){
        if ( document.createEvent ) {
            var evt = document.createEvent('MouseEvents');
            evt.initEvent('click', true, false);
            node.dispatchEvent(evt);    
        } else if( document.createEventObject ) {
            node.fireEvent('onclick') ;    
        } else if (typeof node.onclick == 'function' ) {
            node.onclick();    
        }
    }
     
    var theNode = document.getElementById('watch-like');
    fireClick(theNode);
    
     
    • Thanks Thanks x 3
    Last edited: Apr 18, 2013
  3. ultra.marine

    ultra.marine Registered Member

    Joined:
    Oct 5, 2012
    Messages:
    80
    Likes Received:
    101
    Location:
    Macedonia
    Maybe it's too late for reply, but anyway, I think that pressing that element will not trigger thumb ups, while doing it manually you press 2 or 3 elements in the same time.
    So you need to code that as well, anyway it happens that I've made already something like that so if you're still looking for it let me know and i'll be glad to send you that piece of code in pm.
     
    • Thanks Thanks x 3
  4. vebxperts

    vebxperts Jr. VIP Jr. VIP Premium Member

    Joined:
    Nov 15, 2008
    Messages:
    1,657
    Likes Received:
    339
    PMed you mate as I am stuck at this same situation using webclient!
     
  5. vebxperts

    vebxperts Jr. VIP Jr. VIP Premium Member

    Joined:
    Nov 15, 2008
    Messages:
    1,657
    Likes Received:
    339
    Can you please guide how to implement it?