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

Boss
  • 43
  • 6

4 Answers4

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

Vityata
  • 42,633
  • 8
  • 55
  • 100
  • Thanks for the quick reply, This line throw a compile error: Expected end statement – Boss Apr 10 '20 at 14:53
  • 1
    This one didn't throw any error, and also. It is not click the link text. Thanks! – Boss Apr 10 '20 at 15:05
  • @QHarr - thanks, true, lately I am doing almost no VBA, python mainly. And I have not tried this, there could be other issues... – Vityata Apr 10 '20 at 15:10
  • 1
    Same here @Vityata :-) – QHarr Apr 10 '20 at 15:18
  • I got a ID and the code at now HTMLDoc.getElementById("Result Row").getElementsByTagName("a")(0).Click still throwing error "Object Variable or with block variable not set" :( – Boss Apr 10 '20 at 15:45
  • @Boss - select the `HTMLDoc` in the VBEditor, press Shift + F9 and examine the structure there. Then fix the code correspondingly. The error you are referring to has probably something to do with the initialization of `HTMLDoc`. – Vityata Apr 10 '20 at 15:56
  • @Vityata the window shows as the value – Boss Apr 10 '20 at 16:09
  • @Boss - then the problem is that the `HTMLDoc` is not set correctly. Try to fix it somehow. I will edit the answer, probably the `ie` is not set correctly. – Vityata Apr 10 '20 at 16:50
  • The HTMLDoc like as below Dim ie as object Dim HTMLDoc as MSHTML.HTMLDocument Set HTMLDoc=ie.Document – Boss Apr 10 '20 at 17:00
  • @Boss - it is a good idea to put all the relevant code in the question. Thus, the error would be replicable. Mention also the libraries you are using in the VBEditor. – Vityata Apr 10 '20 at 17:09
  • 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 – Boss Apr 10 '20 at 17:18
  • 1
    @Vityata.. I just add the code in the question. Thanks – Boss Apr 10 '20 at 17:29
  • @Boss - welcome. Consider adding the usage of `Microsoft HTML Object Library`, more people would be able to replicate it this way. – Vityata Apr 10 '20 at 18:21
  • True, it is added, still it is a good idea to put it as an info in the question. I have edited the question, trying to make it work up to the moment the `Set HTMLDoc = ie.Document` fails. Try to do something from there. – Vityata Apr 10 '20 at 18:30
  • @Vityata..... For a change .... am trying to scrap the link "gamit_main.htm?gamitId=168592" from the HTML ... Is it possible. How ever... I need to work on the navigated page only .... Can you help me on this – Boss Apr 11 '20 at 17:16
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

QHarr
  • 83,427
  • 12
  • 54
  • 101
  • Thanks for the reply... Through this code I got error like Run-time error'91': "Object Variable or with block variable not set " – Boss Apr 10 '20 at 15:14
  • 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? – QHarr Apr 10 '20 at 15:18
  • And this the code for the table 1 19-MAR-2020 '70F934C8 – Boss Apr 10 '20 at 16:05
  • That is a different html segment – QHarr Apr 10 '20 at 17:35
  • htmlDoc.querySelector("[title='Circuit Number'] a").click you could try for that but you need to check whether adding a pause before the click resolves the problem and check whether element is inside a parent iframe or frame tag, – QHarr Apr 10 '20 at 17:37
  • Can you please explain how to check the element is inside a parent iframe or frame tag, htmlDoc.querySelector("[title='Circuit Number'] a").click..... Still error as " Run-time error'91': "Object Variable or with block variable not set " " – Boss Apr 10 '20 at 17:47
  • 1) Did you try adding a pause before the click? 2) In the elements tab of browsers see if your target node is nested inside a higher level – QHarr Apr 10 '20 at 20:24
  • 1
    1. Yes. am tried a time delay before the click 2. I am not sure. I Will add the code in question – Boss Apr 11 '20 at 09:18
  • You need to look further up the tree (DOM). If you press Ctr + F in the elements tab and enter the text "iframe" or "frame" then hit enter, you can cycle through any matches and check where they are and whether your node is inside one. – QHarr Apr 11 '20 at 12:12
  • Hello QHarr.... I just found one query asked by MDI this is the link "https://stackoverflow.com/questions/51509597/get-href-link-from-html-page-and-navigate-to-that-link-using-vba". In my case Am also trying the same. In MDI case you only share some idea. I hope in this Case you can help me out... Can you please – Boss Apr 11 '20 at 16:47
  • FYI I have tried this combinations..... 'ie.Navigate2 HTMLDoc.querySelector("#Circuit Number a").href 'ie.Navigate2 HTMLDoc.querySelector("[title='Circuit Number'] a").href 'HTMLDoc.querySelector(".rtabletext [href*='gamitId=']").Click 'HTMLDoc.querySelector(".rtabletext [href*='gamitId=']").href 'ie.Navigate2 HTMLDoc.querySelector(".rtabletext [href*='gamitId=']").href ... But nothing succeed – Boss Apr 11 '20 at 17:21
  • Have you verified no parent frame/iframe ? – QHarr Apr 12 '20 at 11:43
  • I Think It is "iframe"... I will add some snap for more details – Boss Apr 12 '20 at 14:13
1

This code is working fine for Me:

set ElementCol=IE.Document.frames("Content_iFrame").Document.all
For each Link in ElementCol
Boss
  • 43
  • 6
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
sam092
  • 1,325
  • 1
  • 8
  • 8
  • 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 – Boss Apr 10 '20 at 15:50
  • The code like: 1 19-MAR-2020 '70F934C8 – Boss Apr 10 '20 at 16:11