2017-02-03 106 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