VBA-HTML元素单击
我试图从VBA点击IE中的HTML下拉菜单。现在,我实际使用Sendkeys“{Tab}”超过21次来访问元素,然后我使用Sendkeys“{Enter}”来获取下拉列表。显然,这是一个可怕的解决方案,但我似乎无法让其他任何东西起作用 以下是我要单击的元素的HTML代码:VBA-HTML元素单击,html,vba,Html,Vba,我试图从VBA点击IE中的HTML下拉菜单。现在,我实际使用Sendkeys“{Tab}”超过21次来访问元素,然后我使用Sendkeys“{Enter}”来获取下拉列表。显然,这是一个可怕的解决方案,但我似乎无法让其他任何东西起作用 以下是我要单击的元素的HTML代码: <tr> <td height='21'></td> <td colspan='5' valign='top' align='left'> <DIV id='win0di
<tr>
<td height='21'></td>
<td colspan='5' valign='top' align='left'>
<DIV id='win0div$ICField28$0'><table cellpadding='0' cellspacing='0' cols='1' class = ' ' id='$ICField28$scrolli$0' width='948'>
<tr><td><DIV id='win0divGP$ICField28$0'><table cellspacing='0' cellpadding='0' border='0' width = '100%' class='PSLEVEL1SCROLLAREAHEADER' style = 'border:0'><tr><td class='PSLEVEL1SCROLLAREAHEADER' align='left' id = 'PSCENTER'><table class='PSRIGHTCORNER' cellspacing='0' cellpadding='0' border='0' style ='height:100%;' width='100%' ><tr><td class='PSLEVEL1SCROLLAREAHEADER PSLEFTCORNER' style = 'border:0;padding-left:0px;' align='left' ><a name='$ICField28$expand$0' id='$ICField28$expand$0' tabindex='71' href="javascript:submitAction_win0(document.win0,'$ICField28$expand$0');"><img src='/cs/fsprd/cache/PT_EXPAND_1.gif' alt='Expand section Prepayment Penalty' title='Expand section' border='0' /></a> Prepayment Penalty </td>
</tr></table></td></tr></table></DIV></td></tr>
但是我们没有运气
有人知道我可以做些什么来点击这个下拉列表吗?如果我能提供更多信息,请告诉我。试试这个:
Dim IE As Object
Dim img As HTMLImg
Dim i As Integer
Set IE = CreateObject("internetexplorer.application")
IE.navigate "yourwebsite"
Set HTMLdoc = IE.Document
Set img = Nothing
i = 0
While i < HTMLdoc.images.Length And img Is Nothing
If HTMLdoc.images(i).alt = "Expand section Prepayment Penalty" Then Set img = HTMLdoc.images(i)
i = i + 1
Wend
If Not img Is Nothing Then
img.parentElement.Focus
img.parentElement.click
Else
MsgBox "Image title not found"
End If
这是另一种方法。基本上,它填充了一组img标记,然后从中遍历每个标记,查找src何时匹配
Option Explicit
Sub findElementBySrc()
Dim IE As Object
Dim element As Object
Dim elements As Object
Set IE = CreateObject("InternetExplorer.Application")
'Find all the img Tags, this is in a collection
Set elements = IE.document.getElementsByTagName("img")
'iterate over the collection to find an item -
'that matches the src property
For Each element In elements
On Error Resume Next ' to skip over elements without a src property
If element.src = "/cs/fsprd/cache/PT_EXPAND_1.gif" Then
element.Focus
element.Click
'element.FireEvent ("OnClick") 'commented out, sometimes needed
Exit For
End If
Next
Set IE = Nothing
End Sub
尝试更新后的帖子。请确保将对Microsoft HTML对象库的引用设置为MSHTML.htmldocument注释掉'Dim HTMLdoc当您浏览HTMLdoc.images时,它是否返回任何内容?图像是否没有与之关联的id或名称?不,不这样认为,它对我来说工作正常,需要查看图像的网页截图代码。我还将HTMLdoc.images(I).Title更改为HTMLdoc.images(I).Title,因为您的图像代码显示“Title”,请查看是否有帮助。是否还有其他图像具有相同的标题“Expand section”?我有一种感觉,你的页面上有5个其他图像具有相同的标题,因此,当代码设置img时,它设置在错误的图像上,这是最后一个标题为“展开部分”的img,因此没有单击正确的图像。这是有道理的,但由于某些原因无法工作。。。我们可能需要查看HTML。可能元素在一个框架中,等等。如果没有看到页面代码,很难说。我几乎可以保证元素在一个框架中。在这种情况下,我将如何捕获它?我将尝试使用IE提供的元素检查器来提取代码。您应该看到DOM中的路径,才能到达elect。您应该会看到frame或iframe。在这种情况下,我是否需要以某种方式选择框架才能选择元素?这是什么样子的?
Dim IE As Object
Dim i As Integer
Set IE = CreateObject("internetexplorer.application")
IE.navigate "yourwebsite"
Set HTMLdoc = IE.Document
For each l in HTMLdoc.getElementsByTagName("a")
If l.ClassName = "PSLEVEL1SCROLLAREAHEADER PSLEFTCORNER" Then
l.Click
Exit For
End if
Next
Option Explicit
Sub findElementBySrc()
Dim IE As Object
Dim element As Object
Dim elements As Object
Set IE = CreateObject("InternetExplorer.Application")
'Find all the img Tags, this is in a collection
Set elements = IE.document.getElementsByTagName("img")
'iterate over the collection to find an item -
'that matches the src property
For Each element In elements
On Error Resume Next ' to skip over elements without a src property
If element.src = "/cs/fsprd/cache/PT_EXPAND_1.gif" Then
element.Focus
element.Click
'element.FireEvent ("OnClick") 'commented out, sometimes needed
Exit For
End If
Next
Set IE = Nothing
End Sub