Boss
Boss

Reputation: 43

How to click HTML link using VBA

I am trying to click a link in a website as showed in screenshot below. This link's HTML code is like <a target="_blank" href="gamit_main.htm?gamitId=163734">'3311-10310</a>.

Sample Screenshot

So that I have a code to click the link like:

Set HTMLDoc = ie.Document
HTMLDoc.getElementsByTagName("a").Click

But I got error as:

"Object doesn't support this property or method".

Ref code snap:

Code Screenshot

The full code:

Sub Something()
    Dim ie As Object
    Dim HTMLDoc As MSHTML.HTMLDocument
    Dim ckt_No As String
    ckt_No = Range("A2").Value
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ShowWindow ie.Hwnd, SW_MAXIMIZE
    ie.Navigate "http://xyw.htm"
    Do While ie.Busy = True Or ie.ReadyState <> 4: DoEvents: Loop
    ie.Navigate "http:dgetcjdm_ckt_No&display_content=Y&noFormFields=Y&refresh=Y"
    Do While ie.Busy = True Or ie.ReadyState <> 4: DoEvents: Loop
    Set HTMLDoc = ie.Document
    HTMLDoc.getElementById("resultRow").getElementByTagName("a")(0).Click
End Sub

The HTML Code as follows:

        <td align="center" height="15" title="1" class="tdborder">

            <font class="rtabletext">1</font>

        </td>



        <td align="Left" title="GAMITARM Status Date" class="tdborder" nowrap="">

            <font class="rtabletext">19-MAR-2020</font>

        </td>

        <td align="Left" title="Circuit Number" class="tdborder" nowrap="">

            <font class="rtabletext"><a target="_blank" href="gamit_main.htm?gamitId=168592">'70F934C8</a></font>

        </td>

        <td align="Left" title="CUSTOMER" class="tdborder" nowrap="">

            <font class="rtabletext">MICROSOFT CORPORATION</font>

        </td>

        <td align="Left" title="Customer Id" class="tdborder" nowrap="">

            <font class="rtabletext">8684</font>

        </td>


        <td align="Left" title="AO AM" class="tdborder" nowrap="">

            <font class="rtabletext">&nbsp;</font>

        </td>

        <td align="Left" title="AO SD" class="tdborder" nowrap="">

            <font class="rtabletext">&nbsp;</font>

        </td>

        <td align="Left" title="AO ED/AVP" class="tdborder" nowrap="">

            <font class="rtabletext">&nbsp;</font>

        </td>

        <td align="Left" title="AO VP" class="tdborder" nowrap="">

            <font class="rtabletext">&nbsp;</font>

        <td align="Left" title="LCON Phone Contact (SM Feed) " class="tdborder" nowrap="">

            <font class="rtabletext">&nbsp;</font>

        </td>

        <td align="Left" title="LCON Cell Contact (SM Feed) " class="tdborder" nowrap="">

            <font class="rtabletext">&nbsp;</font>


        </td>

        <td align="Left" title="Programme Office Status" class="tdborder" nowrap="">

            <font class="rtabletext">New</font>


        </td>

        <td align="Left" title="Major Initiative" class="tdborder" nowrap="">

            <font class="rtabletext">Ethernet</font>

        </td>

        <td align="Left" title="Project" class="tdborder" nowrap="">

            <font class="rtabletext">APAC Singapore Ethernet Tail Rolls</font>

        </td>

        <td align="Left" title="Phase" class="tdborder" nowrap="">

            <font class="rtabletext">x.2.2.a</font>


        </td>

        <td align="Left" title="LOA received" class="tdborder" nowrap="">

            <font class="rtabletext">NO</font>

        </td>

        <td align="Left" title="Technical Connectivity details received" class="tdborder" nowrap="">

            <font class="rtabletext">NO</font>

        </td>

    </tr>

and am trying to focus on this line

            <font class="rtabletext"><a target="_blank" href="gamit_main.htm?gamitId=168592">'70F934C8</a></font>

Here some snap for parent: Plz find the Highlighted elements

Iframe_Highlight Iframe_TagName -a_Highlight

Upvotes: 2

Views: 1053

Answers (4)

Boss
Boss

Reputation: 43

This code is working fine for Me:

set ElementCol=IE.Document.frames("Content_iFrame").Document.all
For each Link in ElementCol

Upvotes: 1

Vityata
Vityata

Reputation: 43565

The getElementsByTagName() method returns a collection of all elements in the document with the specified tag name, as an HTMLCollection object.

Thus, you should indicate, which element from the collection should be clicked. In this case, the first one is probably the safest choice:

Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")    
Set HTMLDoc = ie.Document
HTMLDoc.getElementsByTagName("a")(0).Click

And by clicking F12 in Chrome, you may examine the website. If that element, you want to click has an ID tag in it, then it is a good idea to specify it:

HTMLDoc.getElementById("SomeFancyID").getElementsByTagName("a")(0).Click

Edit:

As far as the error is in the Set HTMLDoc, then try to minimize the code as much as possible and debug from there. Try this:

Sub Something()

    Dim ie As Object
    Dim HTMLDoc As MSHTML.HTMLDocument

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Navigate "https://stackoverflow.com/questions"

    Do While ie.Busy = True Or ie.ReadyState <> 4: DoEvents: Loop
    Set HTMLDoc = ie.Document

    Stop 'Press SHIFT + F9 and examine the window...

End Sub

And once the code stops on the Stop line, press SHIFT + F9 and examine the window. There you would see all the collections: enter image description here

Upvotes: 1

QHarr
QHarr

Reputation: 84455

I would combine a class selector for the parent node with an attribute = value selector (using contains operator * ) to target the child a tag by its href by it's value

htmlDoc.querySelector(".rtabletext [href*='gamitId=']").click

No need to retrieve more than one node or to use a loop

This does assume first node matched is the desired one. You can always extend the href value used between the '' to be more selector (or more generally extend the css selector used by querySelector to target exact node).

css selectors

Upvotes: 1

sam092
sam092

Reputation: 1335

getElementsByTagName("a")

is returning a collection of a tags

You have to loop through the collection and find the one you want (or if you know exactly the position of that tag in the DOM, then you could directly access that from the collection).

For example,

For each a in ie.document.getElementsByTagName("a") 
    If a.ClassName = "whatever_it_is" Then
        a.Click
        Exit For
    Next

Upvotes: 0

Related Questions