简体   繁体   English

使用VBA和Internet Explorer解析HTML

[英]Using VBA and Internet Explorer to Parse HTML

I am trying to use VBA code to click a "Go" button on a website. 我正在尝试使用VBA代码单击网站上的“开始”按钮。 This is the source code. 这是源代码。

 <div class="actions"> 
  <a id="go" href="javascript:void(null);" title="Go"><img src="/images/button-go-smaller.png"></a>
   <a id="reset" href="javascript:void(null);" title="Reset All Fields"><img src="/images/button-reset_all.png"></a>
                    </div>

This is my VBA code: 这是我的VBA代码:

For Each obj In objCollection
     If objCollection(i).ID = "go" Then
        Set objElement = obj
Exit For
    End If
Next obj
objElement.Click

However, on the objElement.Click line, I get an error 91, which means that the "go" action cannot be found. 但是,在objElement.Click行上,出现错误91,这意味着找不到“执行”操作。 Why is that, and how can I access the go button? 为什么会这样,如何访问转到按钮?

What about... 关于什么...

Dim objCollection As Object
Set objCollection = IE.Document.getElementsbyTagName("a")

For Each obj In objCollection
    If obj.ID = "go" Then
        obj.Click
        Exit For
    End If
Next obj

If you have an html object having an Id you can get it direcly with something like this: 如果您有一个带有Id的html对象,则可以像下面这样直接获得它:

Dim GoObj As Object
Set GoObj = IE.Document.getElementbyId("go")
'Only if it was found you click it
If not GoObj is Nothing then
    GoObj.Click
End If

Pay attention to the difference between element in getElementbyId and element s in getElementsByTagName 注意元素之间的差异getElementbyId和要素sgetElementsByTagName

This is a great example of how to do exactly that. 这是一个很好的例子。

Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()

Dim oHTML_Element As IHTMLElement
Dim sURL As String

On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True

Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE

Set HTMLDoc = oBrowser.Document

HTMLDoc.all.Email.Value = "sample@vbadud.com"
HTMLDoc.all.passwd.Value = "*****"

For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next

' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub

Read more at: http://vbadud.blogspot.com/2009/08/how-to-login-to-website-using-vba.html#XwwFWylLQi9rjILC.99 有关更多信息,请访问: http : //vbadud.blogspot.com/2009/08/how-to-login-to-website-using-vba.html#XwwFWylLQi9rjILC.99

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM