Announcement

Collapse
No announcement yet.

vb.Net - Application to click a link on WebBrowser1

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • vb.Net - Application to click a link on WebBrowser1

    Hi There,

    In the middle of building an application and ripping my hair out trying to get this darn area of the application to work.

    All I need the script to do is :-
    1. Find the FIRST <a> tag which contains a Class="story_hide" attribute
    2. Click the link

    I've tried my hardest to get this to successfully work, now I have a headache.

    Here's what my code resulted to before writing this thread:


    Code:
        Private Function GetElementByAttribValue(ByVal tagName As String, ByVal attribName As String, ByVal attribValue As String) As HtmlElement
            Dim elements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName(tagName)
    
            For Each element As HtmlElement In elements
                If element.GetAttribute(attribName) = attribValue Then
                    Return element
                End If
            Next
    
            Return Nothing
        End Function
    Code:
    Private Sub Test()
    Dim aElement As HtmlElement = GetElementByAttribValue("a", "class", "story_hide")
    aElement.InvokeMember("click")
    End Sub
    Any help, much appreciated!!

    Thanks,
    Andy

  • #2
    Re: vb.Net - Application to click a link on WebBrowser1

    Originally posted by AndyBoot View Post
    All I need the script to do is :-
    1. Find the FIRST <a> tag which contains a Class="story_hide" attribute
    2. Click the link
    Strange as it may sound, there is not a straight-forward way to achieve in .Net. (Disclaimer: I tried with 2.0, 3.x might be different.)

    Getting a collection of anchors is simple enough, but for some reason anchor elements do not have easily accessible attributes. One could use COM wrapper to work with IE's mshtml, but the library isn't present in my Vista system so I can't try it.

    Anyhow, you could use regular expressions to search for anchors. Something like "<a([^>])*>([^<])*</a>" would get all the anchors. Just read the match collection, read the desired url and call WebBrowser control's Navigate() method.

    Would Navigate() call be good enough? Have you got a specific reason to simulate link click? If you do, are you trying to automate link cliks in order to hide some content? I'm asking as there might be more reasonable solutions to your problem.

    Take a look at coding forums. Stack Overflow has some discussion about this problem, and refers to CodePlex and regexps. The thread deals with C#, but the idea is the same anyway.

    http://stackoverflow.com/questions/1...-links-using-c

    -vP

    Comment


    • #3
      Re: vb.Net - Application to click a link on WebBrowser1

      Originally posted by vonPryz View Post
      Would Navigate() call be good enough? Have you got a specific reason to simulate link click? If you do, are you trying to automate link cliks in order to hide some content? I'm asking as there might be more reasonable solutions to your problem.
      Yes pretty much, I'm making a small windows based application for Facebook which interacts with your personal status, which is done and working with iTunes at this moment in time.
      However, I don't want the profile page to be spammed with stories/post's. So by clicking the first link it finds with Class="story_hide" in it will erase the previous one and then update it with a new one.

      If it was for one of my own webpages, I could probably get around this by automating it with a little "on the side" javascript to perform the task, but as it's Facebook I just have no control over it.

      Comment

      Working...
      X