Automate IE with VBA - Click Javascript Link (no Anchor tag)

18,033

Looking at your code, I see

elementList = ie.document.frames(myFrame).document.getElementsByTagName("span")

Which should be used with Set, like so:

Set elementList = ie.document.frames(myFrame).document.getElementsByTagName("span")

As far as the error on the Dim line, you can always just define this as an Object or Variant and this should still work, since the type being returned by getElementsByTagName should still be valid.


Responding to your update, to click a button, I use:

Set objButton = IEPage.getelementbyid("buttonname")
IEPage.onmousedown 'There is JS code that catches these actions for validation, so we artificially trigger them on the page. Your requirements may vary.
objButton.Focus
objButton.Click

Mixing with your code, I would put it together like this (untested):

With ie
 For i5 = 0 To ie.document.frames(myFrame).document.all.tags("SPAN").Length - 1
  With .document.frames(myFrame).document.all.tags("SPAN").Item(i5)
   If InStr(.innerText, "Target") > 0 Then
    Debug.Print .innerText
    ie.document.frames(myFrame).document.onmousedown
    .Focus
    .Click
    Exit For
   End If
  End With
 Next i5
End With
Share:
18,033
Aaron Contreras
Author by

Aaron Contreras

I'm an analyst for a small department with a Fortune 500 company. I work primarily in Excel, and build tools that focus on automation.

Updated on June 04, 2022

Comments

  • Aaron Contreras
    Aaron Contreras almost 2 years

    Our company uses a browser-based program for business operations. My goal is to grab some data out of this system automatically.

    The site itself uses frames pretty heavily, but I'm doing pretty decently handling that. The problem in front of me now is navigating to the screen on which my data is housed.

    The link itself is coded in javascript, and I don't see an anchor tag (The image is a +/-, or invisible for spacing):

    Source Code

    <div id='Nav_4' aTag='aTarget'  title='Target' aUrl="javascript:top.aaa.submitPage('NavigateTo','~/aPages/aPage.aspx?UC=a')">
     <img alt='' style="margin-left:0px; width:0px "/>
     <img src='/a/a.axd?d=a' alt='(Collapsed)' aAltX='(Expanded)' imgType='exp' />
     <img alt=''style='width:5px; visibility:hidden;'>
    <span atxt='1' class="   breadcrumbTreeItem">
     Target
    </span></div>
    

    Since I couldn't get the a tag, or search the document for Links, I instead tried to find the span tag that contained "Target" and try to activate it.

    Here is the working code! (i5 is an iterator)

    Set ie = GetOpenIEByURL("https://aaa.com/AAA.htm")
    fIter = 0
     For Each frmSet In ie.document.getElementsByTagName("Frame")
      If Left(frmSet.src, 7) = "aaa/AAA" Then
       myFrame = f_Iter
       Exit For
      End If
    f_Iter = f_Iter + 1
    Next
    
    With ie
     For i5 = 0 To ie.document.frames(myFrame).document.all.tags("SPAN").Length - 1
      With .document.frames(myFrame).document.all.tags("SPAN").Item(i5)
       If InStr(.innerText, "Target") > 0 Then
        .Click
        Exit For
       End If
      End With
     Next i5
    End With
    

    Additionally, in the Module, add this code:

    'Finds an open IE site by checking the URL
    Function GetOpenIEByURL(ByVal i_URL As String) As InternetExplorer
    Dim objShellWindows As New ShellWindows
    
      'ignore errors when accessing the document property
      On Error Resume Next
      'loop over all Shell-Windows
      For Each GetOpenIEByURL In objShellWindows
        'if the document is of type HTMLDocument, it is an IE window
        If TypeName(GetOpenIEByURL.document) = "HTMLDocument" Then
          'check the URL
          If Left(GetOpenIEByURL.document.URL, 30) = Left(i_URL, 30) Then
            'leave, we found the right window
            Exit Function
          End If
        End If
      Next
    End Function
    
  • Aaron Contreras
    Aaron Contreras almost 11 years
    I figured it out! Your initial feedback set me on the right path! :)
  • Gaffi
    Gaffi almost 11 years
    @AaronContreras Glad I could help! I've learned from my experiences that JS Automation through VBA can be tricky. I have one more piece of advice: split your button clicks, etc into specialized Subs if you'll be doing a lot of them. It'll save a lot f headache. ;-)