2013-03-07 51 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

相关问题