Search code examples
vbainternet-explorergetelementbyidgetelementsbytagname

Click on picture or link behind it


Hi I have macro which supposed to click on button on web page..problem is there is no ID behind just this code. Its intranet web page.

Begining of my code

    Set IE = New InternetExplorerMedium

    IE.Navigate = "some website"

        While IE.ReadyState <> READYSTATE_COMPLETE
          DoEvents
        Wend

IE.Visible = True

    IE.document.getElementById("CategoryCombo").Value = "78"
    IE.document.getElementById("CategoryCombo").FireEvent ("onchange")

'~~> wait until element is present on web page
    Do

        Set ieobj = Nothing

    On Error Resume Next

        Set ieobj = IE.document.getElementById("text1265")

        DoEvents

    Loop Until Not ieobj Is Nothing

'~~> search box
   IE.document.getElementById("text1265").Value = "some value"

'~~> button click example
   IE.document.getElementById("subBtn").Click

Part of website code

<a title="View document" href="javascript:FSResults_fsopenWindow('index.fsp?pn=DOLViewDocument&amp;d=78&amp;q=78-17158635-1&amp;o=78-17158635-1&amp;p=DOCUMENT_NAME#ensureVisible')" onclick="fsCancelEvent(event)"><img border="0" alt="View document" align="absmiddle" src="Images/DocTypes/PDF.gif"></a>

I tried

   IE.document.getElementByTitle("View document").FireEvent ("onclick")

I also tried

   IE.document.getElementByTagName("a").FireEvent ("onclick")

Thank you


Solution

  • I also tried

    IE.document.getElementByTagName("a").FireEvent ("onclick")

    This should work for you, but you are missing something. You need to specify which <a> tag you are looking for, so .getElementsByTagName("a") turns into .getElementsByTagName("a")(i), where i is the index of the tag you are looking for (you get the index by counting in order every <a> tag in the HTML, starting from 0).

    The index is needed because .getElementsByTagName("tagName") returns an array, not a single value.