簡體   English   中英

使用VBA和Internet Explorer解析HTML

[英]Using VBA and Internet Explorer to Parse HTML

我正在嘗試使用VBA代碼單擊網站上的“開始”按鈕。 這是源代碼。

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

這是我的VBA代碼:

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

但是,在objElement.Click行上,出現錯誤91,這意味着找不到“執行”操作。 為什么會這樣,如何訪問轉到按鈕?

關於什么...

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

如果您有一個帶有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

注意元素之間的差異getElementbyId和要素sgetElementsByTagName

這是一個很好的例子。

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

有關更多信息,請訪問: 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