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

由于我无法得到一个标签,或search文档的链接,我试图find包含“目标”的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 

看看你的代码,我明白了

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

其中应该使用Set ,如下所示:

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

至于Dim行上的错误,你总是可以把它定义为一个ObjectVariant ,这应该仍然有效,因为getElementsByTagName返回的types应该仍然有效。


响应你的更新,点击一个button,我使用:

 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 

与你的代码混合,我会把它像这样(未经testing):

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