在网页上以login表单input用户名和密码

使用Excel VBA,我试图login到一个网站。

这是我的代码:

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浏览器打开,但它不进入用户/通过,然后单击button。

请帮忙吗?

我刚刚testing这个代码,它的工作。

唯一的区别是我使用了Late Binding (懒得设置所有引用),并且While .Busy循环中joinWhile .Busy来等待URL的加载。 我也改变了input到button点击循环中的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