我试图将用VBA编写的脚本迁移到我已经在VB.NET中编写的应用程序中,作为附加功能。通过使用VB.NET填充HTML元素自动登录页面
我知道VBA和VB.NET语言差异很大,我一直在努力迁移我需要的代码段。
我需要打开一个Internet Explorer页面,填写两个用于用户名和密码的文本框,然后自动按下提交/登录按钮。
在VBA中,我能够使用下面的代码来完成此操作。
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
'Ignore errors
On Error GoTo Err_Clear
MyURL = Address
Set MyBrowser = New InternetExplorer 'Open Internet Explorer
MyBrowser.Silent = True 'Silence popups & Errors
MyBrowser.Navigate MyURL 'Browse
MyBrowser.Visible = True 'Obvious
Do
'Wait till finished
Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.Document
'Enter user/password
For Each MyHTML_Element In HTMLDoc.getElementsByTagName("input")
If MyHTML_Element.Name = ("username") Then
MyHTML_Element.Value = Username 'Variable to be passed to sub
ElseIf MyHTML_Element.Name = ("password") Then
MyHTML_Element.Value = Password 'Variable to be passed to sub
ElseIf MyHTML_Element.Type = "submit" Then
MyHTML_Element.Click: Exit For
End If
Next
Err_Clear:
If Err <> 0 Then
'Debug.Assert Err = 0
Err.Clear
Resume Next
End If
当这个移动到VB.NET,甚至加入我会使用Microsoft Internet控制的参考文献后,和Microsoft HTML对象库,我收到几个错误,例如:
Dim MyHTML_Element As IHTMLElement '"ITHMLElement" is ambiguous in the namespace 'mshtml'
Loop Until MyBrowser.Readystate = READYSTATE_COMPLETE 'Not declared
我以前使用过Visual Basic,但没有用它来操作Web浏览器中的HTML页面的值,并且在VB.NET在线上一直没能找到关于这个的更多信息。
我希望有人可以提供一些对我的洞察,或指向我在正确的方向,在此先感谢!