2017-08-28 88 views
-1

我一直在嘗試製作一個vb.net程序,可以自動對視頻進行評論。到目前爲止,我得到了:點擊Youtube上的評論按鈕

Public Class Form1 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     WebBrowser1.Navigate("https://www.youtube.com/watch?v=0ZZquVylLEo") 
    End Sub 

現在我需要讓它點擊網站上的評論框。我認爲這樣做的方式是 -

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    WebBrowser1.Document.GetElementById(".comment-simplebox-renderer-collapsed-content").InvokeMember("click") 
End Sub 

但是,這只是給出了一個錯誤。我真的失去了如何做到這一點,所以任何幫助將不勝感激。

+0

閱讀[問] ..請包括所有的細節,這一點至關重要。什麼錯誤?如果你不努力,我們不太可能。 –

+0

HttpWebRequest是做你想做的最好的方法。我真的不喜歡WebBrowser這種情況。 – Youssef13

+1

我不認爲youtube會對此感到滿意。他們提供了一個API來做到這一點。 –

回答

0

添加一個WebBrowser到項目,並將其命名爲「瀏覽器」

browser.Navigate("https://www.youtube.com/watch?v=OQ4oaLUilBc") 
    While browser.ReadyState <> WebBrowserReadyState.Complete 
     Application.DoEvents() 
    End While 
    Dim comment_wrote As Boolean = False 
    Dim comment_done As Boolean = False 
    While comment_wrote = False 
     For Each altelm As HtmlElement In browser.Document.GetElementsByTagName("DIV") 
      If altelm.GetAttribute("classname").ToString = "comment-simplebox-renderer-collapsed comment-simplebox-trigger" Then 
       altelm.Focus() 
       altelm.InvokeMember("click") 
      End If 

      If altelm.GetAttribute("classname").ToString = "comment-simplebox-text" And comment_wrote = False Then 
       altelm.Focus() 
       altelm.InvokeMember("click") 
       SendKeys.SendWait("I love this song!") 
       comment_wrote = True 
      End If 

     Next 
     Application.DoEvents() 
    End While 
    While comment_done = False 
     For Each altelm As HtmlElement In browser.Document.GetElementsByTagName("BUTTON") 
      If altelm.GetAttribute("classname").ToString = "yt-uix-button yt-uix-button-size-default yt-uix-button-primary yt-uix-button-empty comment-simplebox-submit yt-uix-sessionlink" And comment_done = False Then 
       altelm.Focus() 
       altelm.InvokeMember("click") 
       comment_done = True 
      End If 
      Application.DoEvents() 
     Next 
     Application.DoEvents() 
    End While