0

I am very new to VBA and had a question regarding how to click an href link in Internet Explorer. There are multiple href's on the source page. I have never encountered this and it has been giving me a hard time! I have looked on this website searching for answers but decided to ask here.

Below I have listed the code I have, up to the point where I encounter the problem, as well as the Source Code on Internet Explorer.

I commented out what I have tried and listed the error I received.

Code Below:

 Sub ()
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object

User = "User"
Pwd = "Pwd"


Set IE = CreateObject("InternetExplorer.Application")

IE.Visible = True

URL = "URL.com"

IE.Navigate URL

Do While IE.ReadyState <> 4
  DoEvents
Loop

  IE.Document.getElementById("txtUsername").Value = User
  IE.Document.getElementById("txtPassword").Value = Pwd
  IE.Document.getElementById("btnSubmit").Click


  'IE.getElementByClassName("txtTerms").Click - Runtime Error 438
  'IE.getElementByTagName("Claims Management").Click - Runtime Error 438

'Set HREF = IE.Document.getElementsByClassName("txtTerms")
    'For Each HREF In IE.Document.getElementsByTagName("Claims").Click  - No error occurs, nothing happens.

End Sub

Internet Explorer Source Code:

<table id="tblContent">
<tr>
<td class="txtTerms"><a href='href url 1'>Claims</a>

<br>Download<br>Create<br><a class='terms' href='href url 2' 
 target='terms'>Terms</a><br><br></td>
</tr>

My question would be, how to get VBA to click only on 'href url 1'?

Let me know if any additional information is needed. I apologize for my level of VBA but I am excited to learn more!

Thanks for the help!

Ibo
  • 4,081
  • 6
  • 45
  • 65
Novice_VBAUser
  • 1
  • 1
  • 1
  • 2
  • Refer to [this answer](https://stackoverflow.com/a/47333783/7690982) for some guidance. – danieltakeshi Dec 18 '17 at 18:17
  • Thank you for the guidance Daniel! That was very informal, I appreciate it. However, I forgot to mention the 'href url 1' and 'href url 2' are not static and change every time I login in. Is there a different way to click the 'href url 1' using/trying a different method? – Novice_VBAUser Dec 18 '17 at 20:42
  • If what you want is the first and second href each time you open a website, you could put a counter inside the loop `i=i+1` and then Exit the loop when `i=1` with `If i = 1 Then Exit For` – danieltakeshi Dec 19 '17 at 10:30

1 Answers1

1

In HTML, href is a property of the type <a> (link) which contains an absolute or relative path. For example:

<a href="/questions/">Questions</a>

... will show as "Questions" and, if you click it, will bring you to www.stackoverflow.com/questions/. Note that "www.stackoverflow.com" has been added automatically since the path is relative.

<a href="https://www.facebook.com">Facebook</a>

... will show as "Facebook" and, if you click it, will bring you to www.facebook.com. In this case, the path is absolute.

Although your HTML code is incomplete, I guess that all the links you want to navigate are contained in the table having id="tblContent". If that's the case, then you can get all the links (tagName == 'a') in that table and store the values in a collection:

Dim allHREFs As New Collection
Set allLinks = IE.Document.getElementById("tblContent").getElementsByTagName("a")
For Each link In allLinks
    allHREFs.Add link.href
Next link

You can then decide to navigate them and do what you have to do one by one:

For j = 1 To allHREFs.Count
    IE.Navigate URL + allHREFs(j) '<-- I'm assuming hrefs are relative.
    'do your stuff here
Next href
Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
  • Sorry for the late response. But below is the code that successfully "clicked" the href. I appreciate all the help I was able to get and it really helped!! Thank you. Set html = ie.Document Set ElementCol = html.getElementsByTagName("a") For Each link In ElementCol If link.innerHTML = "Claims Management" Then link.Click Exit For End If – Novice_VBAUser Jan 09 '18 at 16:53