2013-03-07 99 views
0

...我已經尋找如何前點擊VB中的一個網頁鏈接,並得到了在這個網站實際上是一個很好的答案。但現在我想點擊另一個鏈接,並讓我張貼的代碼,所以它更容易理解。 (第一次問一個問題,這樣下去容易對我大聲笑)如何點擊一個網頁鏈接在Visual Basic

<div class="pauseButton" style="display: block;"><a href="#" address="true"></a></div> 

這裏是我的代碼(這是潘多拉順便說一句,這裏是我的代碼,它讓您登錄)。

Public Class fMain 

Dim elementCollection As HtmlElementCollection 

Private Sub fMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    wb.Navigate("http://www.pandora.com") 
End Sub 

'buttons 

Private Sub bLogin_Click(sender As Object, e As EventArgs) Handles bLogin.Click 
    elementCollection = wb.Document.GetElementsByTagName("input") 

    For Each ele As HtmlElement In elementCollection 
     If ele.GetAttribute("name").Equals("email") Then 
      ele.SetAttribute("Value", tbEmail.Text) 
     End If 

     If ele.GetAttribute("name").Equals("password") Then 
      ele.SetAttribute("Value", tbPassword.Text) 
     End If 

     If ele.GetAttribute("type") = "submit" Then 
      ele.InvokeMember("click") 
     End If 
    Next 
End Sub 

Private Sub bSignOut_Click(sender As Object, e As EventArgs) Handles bSignOut.Click 

End Sub 

Private Sub bPlay_Click(sender As Object, e As EventArgs) Handles bPlay.Click 
    Dim IsRightElement As Boolean = False 

    For Each ele As HtmlElement In wb.Document.Links 
     If IsRightElement Then 
      ele.InvokeMember("click") 
      IsRightElement = False 
      Exit For 
     End If 

     If ele.GetAttribute("class") = "playButton" Then 
      IsRightElement = True 
     End If 
    Next 
End Sub 

Private Sub bPause_Click(sender As Object, e As EventArgs) Handles bPause.Click 

End Sub 

Private Sub bFavorite_Click(sender As Object, e As EventArgs) Handles bFavorite.Click 

End Sub 
End Class 

任何幫助將不勝感激,和對不起,如果我提出的問題令人困惑,但幾乎我知道如何點擊與特定的HREF =「link.com」的鏈接,但在這裏,唯一與任何區分播放按鈕其他按鈕就是它的href =「#」,所以沒有太大的幫助。再次感謝先進的。 (:

編輯:我試圖做一個潘多拉流光,我應該提到更早

+0

退房這個帖子:http://stackoverflow.com/a/17595506/381273 – nicky 2013-07-11 14:41:38

+0

@nicky謝謝你,是有很大幫助! – sociallymellow 2013-07-19 04:59:24

回答

0

在HTML我給的鏈接的ID:

<div class="pauseButton" style="display: block;"><a id="LinkID" href="#" address="true"></a></div> 

VB.Net代碼按ID獲取元素並調用它:

Private Function ClickSubmitButton() 

     Dim theButton As HtmlElement 

     Try 

      ' Link the ID from the web form to the Button var 
      theButton = webbrowser1.Document.GetElementById("LinkID") 

      ' Now do the actual click. 
      theButton.InvokeMember("click") 
      Return True 

     Catch ex As Exception 

      Return False 

     End Try 

    End Function 

更新:

好了沒有,你需要通過所有元素循環中的鏈路ID,一旦你確定了DIV,你知道下一個元素是鏈接...

Dim IsRightElement as Boolean = False 
For Each ele As HtmlElement In wb.Document.Links 
     If IsRightElement Then 
      ele.InvokeMember("click") 
      IsRightElement = False 
      Exit For 
     End If 
     If ele.GetAttribute("class") = "playButton" Then 
      IsRightElement = True 
     End If 
    Next 
+0

您好,感謝您的回覆,但是我不能修改網頁,因爲它是pandora.com(所以我可以一個id不添加到按鈕/鏈接) – sociallymellow 2013-03-08 00:41:10

+0

結帳我的更新 – 2013-03-08 00:51:03

0

如果您使用的Visual Studio.NET,這樣做: 1.Drop一個超鏈接控件 2 .Under NavigateUrl屬性,單擊省略號。你現在應該得到一個屏幕,在您的項目 3.Select你的HTML文件中的所有文件。如果不是你的項目的一部分,使用瀏覽添加它。

+0

您好,感謝您的回覆,但因爲它是pandora.com – sociallymellow 2013-03-08 00:40:27

+0

@sociallymellow我不能修改的網頁:哦。抱歉。我以爲你正在寫一個網頁。 – refi64 2013-03-08 23:45:45

+0

沒問題,我仍然欣賞你想(:謝謝 – sociallymellow 2013-03-09 01:26:22

相關問題