使用VBA自动化IE - 单击Javascript链接(无锚标记)

时间:2013-05-21 14:00:29

标签: excel internet-explorer vba excel-vba automation

我们公司使用基于浏览器的程序进行业务运营。我的目标是自动从系统中获取一些数据。

该网站本身使用相当多的帧,但我正在做相当不错的处理。我面前的问题是导航到我的数据所在的屏幕。

链接本身是用javascript编码的,我没有看到锚标记 (图像为+/-,或间距不可见)

源代码

<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>

由于我无法获取标签,或者在文档中搜索链接,我试图找到包含“目标”的span标记并尝试激活它。

这是工作代码! (i5是迭代器)

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

此外,在模块中,添加以下代码:

'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

1 个答案:

答案 0 :(得分:0)

查看您的代码,我看到了

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

哪个应与Set一起使用,如下所示:

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

Dim行上的错误而言,您始终可以将其定义为ObjectVariant,这仍应有效,因为{{返回}的类型1}}应该仍然有效。


响应您的更新,点击按钮,我使用:

getElementsByTagName

与你的代码混合,我会像这样(未经测试)把它放在一起:

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