使用 VBA 自动化 IE - 单击 Javascript 链接(无锚标记)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16671970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automate IE with VBA - Click Javascript Link (no Anchor tag)
提问by Aaron Contreras
Our company uses a browser-based program for business operations. My goal is to grab some data out of this system automatically.
我们公司使用基于浏览器的程序进行业务运营。我的目标是自动从这个系统中获取一些数据。
The site itself uses frames pretty heavily, but I'm doing pretty decently handling that. The problem in front of me now is navigating to the screen on which my data is housed.
该网站本身大量使用框架,但我在处理它方面做得相当不错。现在摆在我面前的问题是导航到我的数据所在的屏幕。
The link itself is coded in javascript, and I don't see an anchor tag (The image is a +/-, or invisible for spacing):
链接本身是用 javascript 编码的,我没有看到锚标记 (图像是 +/-,或者间距不可见):
Source Code
源代码
<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>
Since I couldn't get the a tag, or search the document for Links, I instead tried to find the span tag that contained "Target" and try to activate it.
由于我无法获得 a 标签,或在文档中搜索链接,我尝试找到包含“Target”的 span 标签并尝试激活它。
Here is the working code!(i5 is an iterator)
这是工作代码!(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
Additionally, in the Module, add this code:
此外,在模块中,添加以下代码:
'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
采纳答案by Gaffi
Looking at your code, I see
看看你的代码,我明白了
elementList = ie.document.frames(myFrame).document.getElementsByTagName("span")
Which should be used with Set
, like so:
应该与 一起使用Set
,如下所示:
Set elementList = ie.document.frames(myFrame).document.getElementsByTagName("span")
As far as the error on the Dim
line, you can always just define this as an Object
or Variant
and this should still work, since the type being returned by getElementsByTagName
should still be valid.
就Dim
行上的错误而言,您始终可以将其定义为Object
orVariant
并且这应该仍然有效,因为返回的类型getElementsByTagName
应该仍然有效。
Responding to your update, to click a button, I use:
响应您的更新,单击一个按钮,我使用:
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
Mixing with your code, I would put it together like this (untested):
与您的代码混合,我会将它放在一起(未经测试):
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