我们公司使用基于浏览器的程序进行业务运营。我的目标是自动从这个系统中获取一些数据。
该网站本身大量使用框架,但我处理得相当体面。现在摆在我面前的问题是导航到存放我的数据的屏幕。
链接本身是用 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>
由于我无法获得 a 标签,或在文档中搜索链接,因此我尝试找到包含“目标”的跨度标签并尝试激活它。
这是工作代码! (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