2

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

0

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

13 Comments

Thanks for the reply... Through this code I got error like Run-time error'91': "Object Variable or with block variable not set "
Do you still get this error if you step through with F8 executing code line by line? Are you using xmlhttp or internet explorer? Is the node within a parent iframe or frame node?
And this the code for the table </thead> <tbody class="scrollableTableContent" > <tr id="resultRow" class=rowalt1> <td align=center height=15 title="1" class="tdborder" > <font class=rtabletext>1</font> </td> <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>
That is a different html segment
1. Yes. am tried a time delay before the click 2. I am not sure. I Will add the code in question
|
1

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

15 Comments

Thanks for the quick reply, This line throw a compile error: Expected end statement
This one didn't throw any error, and also. It is not click the link text. Thanks!
@QHarr - thanks, true, lately I am doing almost no VBA, python mainly. And I have not tried this, there could be other issues...
Same here @Vityata :-)
@Vityata.. I just add the code in the question. Thanks
|
1

This code is working fine for Me:

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

Comments

0
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

2 Comments

Thanks for the reply.... I have changed the code like as below For each a in ie.document.getElementsByTagName("a") If a.ClassName = "rtableText" Then a.Click Exit For end if Next a This code didn't throw any error. and also it is not click the link text
The code like: </thead> <tbody class="scrollableTableContent" > <tr id="resultRow" class=rowalt1> <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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.