如何在visual basic中单击网页上的链接

时间:2013-03-07 23:32:28

标签: vb.net click hyperlink

我已经搜索了如何在vb中点击网页上的链接,并且实际上在这个网站上得到了一个很好的答案。但现在我想点击另一个链接,让我发布它的代码,这样更容易理解。 (第一次问一个问题,所以对我来说很容易大声笑)

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

这是我的代码(这是Pandora顺便说一句,这是我签署你的代码。)

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 =“#”因此没有多大帮助。再次感谢先进。 (:

编辑:我正在尝试制作潘多拉流光,我应该早些提到它。

2 个答案:

答案 0 :(得分:0)

如果您使用的是Visual Studio.NET,请执行以下操作: 1.Drop HyperLink控件 2.在NavigateUrl属性下,单击省略号。您现在应该获得包含项目中所有文件的屏幕 3.选择您的HTML文件。如果它不是您项目的一部分,请使用“浏览”添加它。

答案 1 :(得分: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

更新

没有LinkID你需要循环遍历所有元素,一旦你识别出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