【问题标题】:How to click links on a webpage in visual basic如何在visual basic中单击网页上的链接
【发布时间】:2013-03-07 23:32:28
【问题描述】:

我以前在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="#" 所以没有太大帮助。再次感谢先进。 (:

编辑:我正在尝试制作潘多拉流媒体,我应该在前面提到。

【问题讨论】:

标签: vb.net click hyperlink


【解决方案1】:

在 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

【讨论】:

  • 您好,谢谢您的回复,但是我无法修改网页,因为它是pandora.com(因此我无法为按钮/链接添加ID)
【解决方案2】:

如果您使用的是 Visual Studio.NET,请执行以下操作: 1.删​​除一个超链接控件 2.在 NavigateUrl 属性下,单击省略号。您现在应该会看到一个包含项目中所有文件的屏幕 3.选择您的 HTML 文件。如果它不属于您的项目,请使用浏览添加它。

【讨论】:

  • 您好,谢谢您的回复,但是我不能修改网页,因为它是pandora.com
  • @sociallymellow:哦。对不起。我以为你在写网页。
  • 没问题,我仍然感谢您的尝试(:谢谢
猜你喜欢
  • 2019-04-18
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 2014-08-29
  • 2014-01-26
  • 1970-01-01
  • 2014-07-06
  • 1970-01-01
相关资源
最近更新 更多