我们公司使用基于浏览器的程序进行商业运营。我的目标是自动从这个系统中获取一些数据。使用VBA自动化IE浏览器 - 点击Javascript链接(无锚标签)
该网站本身使用框架相当沉重,但我正在做相当体面的处理。现在我面前的问题是导航到我的数据所在的屏幕。
链接本身被编码在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
此外,在该模块中添加以下代码:我看
elementList = ie.document.frames(myFrame).document.getElementsByTagName("span")
应
'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
喜,我用'Set ie = GetOpenIEByURL(“https://aaa.com/AAA.htm”)'得到编译错误。说功能或子没有定义,除了tryint声明值。我选择了IE库。任何想法可能是错的? – Trm
这完全是我的错,我忘记了包含调用的代码!我已经编辑了我的问题以包含上述代码:)祝您好运! –