0

I have searched for how to click links on a webpage in vb before, and got a good answer on this site actually. But now I am trying to click another link, and let me just post it's code so it's easier to understand. (First time ever asking a question, so go easy on me lol)

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

Here is my code (This is for Pandora btw, and here is my code for it to sign you in.)

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

Any help would be greatly appreciated, and sorry if I made the question confusing, but pretty much I know how to click a link with specific href="link.com" but here, the only thing distinguishing the play button with any other button is its the href="#" so that is not much help. Again thanks in advanced. (:

EDIT: I am trying to make a Pandora streamer, I should have mentioned that earlier.

sociallymellow
  • 155
  • 1
  • 2
  • 14

2 Answers2

0

If you're using Visual Studio.NET, do this: 1.Drop a HyperLink Control 2.Under the NavigateUrl property, click the ellipsis. You should now get a screen with all the files in your project 3.Select your HTML file. If it isn't part of your project, add it using Browse.

kirbyfan64sos
  • 10,377
  • 6
  • 54
  • 75
0

In the HTML I give the link an ID:

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

VB.Net code to get the element by Id and invoke it:

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

Update:

Ok without the LinkID you need to loop through all elements, once you identify the DIV, you know the next element is the link...

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
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Hello, thank you for your reply, however I cannot modify the webpage because it is pandora.com (Therefore I cannot add an id to the button / link) – sociallymellow Mar 08 '13 at 00:41