[英]Enter username and password in login form on a webpage
使用 Excel VBA,我正在尝试登录网站。
这是我的代码:
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Website_Login()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
sURL = "https://www.domain.com/login.html"
Set oBrowser = New InternetExplorer
'oBrowser.Silent = True
'oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True
Do
Loop Until oBrowser.ReadyState = READYSTATE_COMPLETE
Set HTMLDoc = oBrowser.Document
With HTMLDoc
HTMLDoc.getElementById("username").Value = "user"
HTMLDoc.getElementById("password").Value = "password"
End With
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
oHTML_Element.Click
Exit For
Next
oBrowser.Quit
End Sub
此代码中的 Javascript 指令正在运行。 如果我转到页面并输入它们,它们就会起作用。 但是当我运行代码时,IE 打开但它没有输入用户/密码并单击按钮。
请问有什么帮助吗?
我刚刚测试了这段代码,它起作用了。
唯一的区别是我使用了Late Binding
(懒得设置所有引用)并且我在循环中添加了While .Busy
以等待 URL 加载。 我还在按钮单击循环中将input
更改为button
。
Dim HTMLDoc As Object 'HTMLDocument
Dim oBrowser As Object 'InternetExplorer
Sub Website_Login()
Dim oHTML_Element As Object 'IHTMLElement
Dim sURL As String
sURL = "https://www.mypage.com/login.html"
Set oBrowser = CreateObject("InternetExplorer.Application") 'New InternetExplorer
'oBrowser.Silent = True
'oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True
With oBrowser
Do While .Busy Or .ReadyState <> 4: Loop
End With
Set HTMLDoc = oBrowser.Document
With HTMLDoc
HTMLDoc.getElementById("username").Value = "user"
HTMLDoc.getElementById("password").Value = "password"
End With
For Each oHTML_Element In HTMLDoc.getElementsByTagName("button")
If oHTML_Element.Name = "Submit" Then oHTML_Element.Click
Exit For
Next
oBrowser.Quit
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.