2017-02-03 185 views
0

我想从VBA中单击IE中的HTML下拉菜单。现在,我实际上使用Sendkeys“{Tab}”超过21次前往元素,然后使用Sendkeys“{Enter}”来获取下拉列表。显然,这是一个可怕的解决方案,但我似乎无法得到任何其他的工作VBA - HTML元素点击

这里是我想点击的元素的HTML代码:

<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>&nbsp;Prepayment Penalty&nbsp;</td> 
</tr></table></td></tr></table></DIV></td></tr> 

我试着做多点击HTML元素的东西如:

Dim IE as object 
Set IE = CreateObject("InternetExplorer.Application") 
IE.document.getelementsbytagname("img")(0).click 

但是没有运气。

有没有人有任何想法,我可以做点击这个下拉?请让我知道我是否可以提供更多信息。

回答

0

试试这个:

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 

也试试这个:

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

我得到一个运行时错误91上线“设置HTMLDOC = IE.Document” 有一定的图书馆中,我需要参考什么加载“对象变量或带块变量未设置”? – Tollbooth

+1

尝试更新后。确保设置对Microsoft HTML对象库的引用。 –

+1

comment'Dim HTMLdoc As MSHTML.HTMLDocument –

0

这是另一种方法。基本上它会填充一组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 
+0

这是有道理的,但由于某种原因不起作用...呃 – Tollbooth

+1

我们可能需要查看HTML。也许元素在框架中,等等。很难说没有看到页面代码。 –

+0

我几乎可以保证元素在一个框架中。如何在这种情况下捕获它?我会试着拉代码 – Tollbooth