[英]Login to website using VB Macro
我正在嘗試使用Macro從站點中提取數據。 下面是我的代碼。 它填寫了ID和密碼,但實際上並未登錄該站點。 需要幫助來確定實際錯誤在哪里。 然后我可以慢慢地進入正確的位置以提取報告。
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub HRALogin()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "website" 'removed actual website due to sensitivity
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.UserName.Value = "abcdefg"
HTMLDoc.all.Password.Value = "1234abcd"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "login" Then oHTML_Element.Click: Exit For
Next
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "login" Then
oHTML_Element.Click
End If
Exit For
Next
Do
'Wait till the Browser is loaded
Loop Until oBrowser.ReadyState = READYSTATE_COMPLETE
另外,在單擊登錄后設置延遲,以使其正確加載頁面,然后執行其余操作。
這個:
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "login" Then oHTML_Element.Click: Exit For
Next
等效於此:
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "login" Then
oHTML_Element.Click
End If
Exit For
Next
即Exit For
不受If
控制。 那可能是你的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.