尝试使用vba单击网页上的button

我正在尝试使用以下代码单击网页上的button。 出于某种原因,我的代码跳到“For Each”行后面。 任何build议将是伟大的! 这里是我的代码片段:

Dim htmldoc As MSHTML.IHTMLDocument 'Document object Dim htmlColl As MSHTML.IHTMLElementCollection Dim htmlInput As MSHTML.HTMLInputElement With ie Set htmldoc = .document Set htmlColl = htmldoc.getElementsByTagName("a") Do While htmldoc.readyState <> "complete": DoEvents: Loop For Each htmlInput In htmlColl **'it skips to End With after this line** If Trim(htmlInput.ID) = "b2" Then htmlInput.Click Exit For End If Next htmlInput End With **'this is where it skips to** 

HTML:

 <TD id=b2><A onclick="selectButton( 'b2' );" onmouseover="window.status='Transoffering'; return true" onmouseout="window.status='View Transmission Offerings';return true" class=button href="/cgi-bin/webplus.dll?script=/woa/woa-transoffering-summary.wml" target=content>Offerings</A><TD> 

该ID属于td ,而不是a ,但你可以使用它来到链接:

编辑:扩大了一点澄清/debugging

 Dim el Set el = htmldoc.getElementById("b2") If el Is Nothing Then Debug.Print "id='b2' not found!" Else el.getElementsByTagName("a")(0).Click End If 

编辑2:如果使用框架

 Set el = htmldoc.frames("frameNameHere").document.getElementById("b2")