Results 1 to 7 of 7

Thread: [RESOLVED] Read Part of an HTML Link and InvokeMember-Click

  1. #1
    Junior Member
    Join Date
    Apr 12
    Posts
    20

    Resolved [RESOLVED] Read Part of an HTML Link and InvokeMember-Click

    I would like to invoke a click on a link from a link that is generated by an HTML query. I am attempting to program an application that queries a website and am getting hung up where I download the .csv that is loaded by the website. Here is the link I am trying to read:
    HTML Code:
    <a href="http://www5.statcan.gc.ca/cansim/results/cansim9170974256855749928.csv">Download file from CANSIM (CSV Version, 5<abbr title="kilobyte">kb</abbr>)</a>
    cansim9170974256855749928 from the url is not static and I don't know how to work around this. Is there a way to enable a clicking event on a random csv link? I have mashed together some failing attempts at this off and on for some time now and am now in your hands. All I have for code to show after all attempts is:
    Code:
    With WebBrowser1.Document
    
    End With
    Cross Posted Here

  2. #2
    Addicted Member
    Join Date
    Jul 09
    Posts
    208

    Re: Read Part of an HTML Link and InvokeMember-Click

    Loop through WebBrowser1.Document.Links (each link is a HTMLAnchorElement) looking for the link with innerText starting with "Download file from CANSIM (CSV Version". Call the Click method on that link.

    Is your program written in VBScript or VBA?

  3. #3
    Junior Member
    Join Date
    Apr 12
    Posts
    20

    Re: Read Part of an HTML Link and InvokeMember-Click

    Hi His Nibbs and thank you for the response. I will give that a go and hopefully all goes well. I am writing this with regular ol VB.NET. I posted in this forum because of the topic matter.

  4. #4
    Junior Member
    Join Date
    Apr 12
    Posts
    20

    Re: Read Part of an HTML Link and InvokeMember-Click

    Ok so I have tried playing around with your suggestion but I'm not sure how to structure this. I have:
    Code:
    Imports System.Windows.Forms.LinkLabel
    and then in the Class Form1 I have:
    Code:
    Dim lnk As Link
    For Each lnk In WebBrowser1.Document.Links
       If WebBrowser1.Document.GetElementById(lnk.ToString).InnerText = "Download file from CANSIM (CSV Version, 5kb)" Then
    
       End If
    Next
    However this throws an error at run time saying "Unable to cast object of type 'System.Windows.Forms.HtmlElement' to type 'Link'."

    I will keep tossing around some thoughts while I work on this but I am still stuck.

  5. #5
    Junior Member
    Join Date
    Apr 12
    Posts
    20

    Re: Read Part of an HTML Link and InvokeMember-Click

    After some digging around on the interwebs I found the following but it doesn't find the innerText value of the link
    Code:
    For Each link As HtmlElement In WebBrowser1.Document.Links
       If link.InnerText = "Download file from CANSIM (CSV Version, 5kb)" Then
          WebBrowser1.Document.GetElementById(link.InnerText).InvokeMember("click")
       End If
    Next

  6. #6
    Junior Member
    Join Date
    Apr 12
    Posts
    20

    Re: Read Part of an HTML Link and InvokeMember-Click

    Woot, this is finally behind me now. Thanks for the kick in the right direction His Nibbs, I have the following code which does the job nicely
    Code:
    For Each link As HtmlElement In WebBrowser1.Document.Links
       If link.InnerText.Contains("Download file from CANSIM") Then
          WebBrowser1.Navigate(link.GetAttribute("href"))
       End If
    Next

  7. #7
    Addicted Member
    Join Date
    Jul 09
    Posts
    208

    Re: Read Part of an HTML Link and InvokeMember-Click

    Quote Originally Posted by MorDred View Post
    I am writing this with regular ol VB.NET. I posted in this forum because of the topic matter.
    You posted in the wrong forum then. The forums are arranged by programming language, not topic. But you got there in the end.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •