2013-05-21 70 views
2

我们公司使用基于浏览器的程序进行商业运营。我的目标是自动从这个系统中获取一些数据。使用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 
+0

喜,我用'Set ie = GetOpenIEByURL(“https://aaa.com/AAA.htm”)'得到编译错误。说功能或子没有定义,除了tryint声明值。我选择了IE库。任何想法可能是错的? – Trm

+0

这完全是我的错,我忘记了包含调用的代码!我已经编辑了我的问题以包含上述代码:)祝您好运! –

回答

0

你的代码展望与Set一起使用,如下所示:

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

至于Dim行上的错误,您始终可以将其定义为ObjectVariant,这应该仍然有效,因为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 

与您的代码混合,我会把它一起像这样(未经):

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

我想通了!你的初步反馈使我走上了正确的道路! :) –

+0

@AaronContreras很高兴我能帮忙!我从我的经验中学习到,通过VBA的JS Automation可能会非常棘手。我还有一条建议:如果你要做很多的话,将你的按钮点击等分成专门的“Sub”。它会节省很多头痛。 ;-) – Gaffi

相关问题