[英]Using VBA to control IE to login to a website
我是新手,但我正在練習,在我看來,登錄我學院的網站應該是世界上最簡單的事情。 對? 好吧,這讓我發瘋。
具體來說,程序在page.Document.getElementById("edit-name").Value = 1234
處失敗
運行時錯誤 424“需要對象”
另外,當我輸入“頁面”時,我不確定這是否相關。 VBA 編輯器提供了一個選項列表,我選擇了文檔。 當我輸入第二個“。” 獲取“page.Document”。 然后我沒有得到進一步的選項列表。
這是完整的程序,感謝您的幫助
Public Sub MyLogin()
'This uses early binding
'In menu at top of page - Tools...Reference.... Microsoft Internet Controls and Microsoft HTML Object Library must both be ticked
Dim page As InternetExplorer
Set page = New InternetExplorer
page.Visible = True
Dim MyLink As Object
'Login at Institute website
page.Navigate "http://www.actuaries.org.uk/"
Do While page.Busy Or page.ReadyState <> 4
DoEvents
Loop
For Each MyLink In page.Document.Links
If InStr(MyLink.href, "/user") Then MyLink.Click: Exit For
Next MyLink
Do While page.Busy Or page.ReadyState <> 4
DoEvents
Loop
'Are now at the login page
'Enter username, password and click
page.Document.getElementById("edit-name").Value = 1234
page.Document.getElementById("edit-pass").Value = "01/01/1977"
page.Document.getElementById("edit-submit").Click
End Sub
具體來說,程序在 page.Document.getElementById("edit-name").Value = 1234 處失敗
那條線沒有錯。
我能想到的一個原因是你的For
循環沒有執行。 為避免這種情況,請直接導航到相關url
。 這個修剪過的版本對我有用。
Sub Sample()
Dim sUrl As String: sUrl = "https://www.actuaries.org.uk/user"
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate sUrl
Do While ie.readystate <> 4: DoEvents: Loop
ie.Document.getElementById("edit-name").Value = 1234
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.