1

I am trying to click on a hyperlink to download a file on a webpage. The HTML shows that it is javascript.

I tried links from StackOverflow and more than four hours worth of research prior to that to no avail.

Here's the latest iteration of my code.

Dim l
For Each l In HTMLdoc.getElementsByTagName("a")
    If l.innerText = "Comma Separated Value (CSV)" Then
        l.Click
        Exit For
    End If
Next l

Here's the HTML for the element I'm trying to click.

<a style="color:#0000FF" href="javascript:JTRANSUM('https://www.merchantconnect.com/CWRWeb/download.do?','CRTRNRPT','1','950******4','01-OCT-2019','03-OCT-2019','7','1216','64***','NONE','0','Z216******','4616**********','Z216******','MID','HIGHEST','CARD_TYPE','HIGHEST','CARD_NBR','HIGHEST','lb4******','N','0','8755***********','COMT');">Comma Separated Value (CSV)</a>

I would like to click on this link by using the text "Comma Separated Value (CSV)".

Community
  • 1
  • 1
DocatWork
  • 11
  • 4
  • Add in `Debug.Print l.innerText` just before the `If` and check what links your code is finding. – Tim Williams Oct 04 '19 at 04:11
  • Is any part of that href attribute string (value after the = ) unique to the csv download button? E.g. _JTRANSUM_ ? – QHarr Oct 04 '19 at 04:18
  • Your code is written in such a way that it could be failing gracefully, too gracefully. It could be as simple as a mistyped character so your condition if never true. The element or your loop might be inside an iFrame so it can't see the element. You can further classify the problem be trying to interact with the element directly and noting the error. The instant and local windows will help troubleshoot so I recommend placing a breakpoint on the line where the element is set to a variable and pressing F8 once. The information gathered there should give you the opportunity to dig deeper. – ProfoundlyOblivious Oct 04 '19 at 05:15
  • Tim, after adding your Debug.Print, I was able to determine that the inner table where the report links are being displayed is not being accessed. All the othe links on the page, outside this frame, are coming back. – DocatWork Oct 04 '19 at 16:44
  • QHarr. Yes, the "Comma Separated Value (CSV)" text is unique. Other than that, no. Nothing that precedes the "...(CSV)" is unique. – DocatWork Oct 04 '19 at 19:45
  • 1
    A hosted frame will contain a separate document from the top-level window - you need to get a reference to the frame and then its document before you can access your link. Eg see https://stackoverflow.com/questions/44902558/accessing-object-in-iframe-using-vba – Tim Williams Oct 05 '19 at 05:19
  • This is what I was talking about with the loop and the element not being able to see each other. Using Selenium I would find the frame by using F12 in the browser and add `SwitchToFrame("iFrame")` in my code, admitedly I don't know the IE equivalent is. – ProfoundlyOblivious Oct 05 '19 at 18:48
  • Thank you, Tim. Unfortunately, I am having problems adapting this to my particular problem. I will continue down the route of figuring out how to access elements of the iframe now that you have gotten me going in the correct direction. – DocatWork Oct 07 '19 at 18:00
  • The thread that Tim providing has already clarified how to access elements in iframe. I also find [another thread](https://stackoverflow.com/questions/54060446/vba-how-to-click-on-element-within-iframe) which may be helpful. You could also refer to it. – Yu Zhou Oct 10 '19 at 02:39

0 Answers0