我正在尝试使用访问IE的VBA宏登录到一个网站,一旦我到达这个URL并使用user和pass登录,然后我希望VBA中的IE对象能够使用弹出窗口的这个新URL来操作它(插入到表单等)。这是我的代码:
Sub Automate_IE_Load_Page()‘这将在IE Dim IE中加载一个网页作为对象Dim URL作为字符串
'Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "http://www.web app that requiers login credentials.com"
'Navigate to URL
IE.Navigate URL
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.ReadyState = 4: DoEvents: Loop 'Do While
Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
'Webpage Loaded
Application.StatusBar = URL & " Loaded"
'Insert login credentials
IE.document.getElementById("UserName").Value = "123"
IE.document.getElementById("Password").Value = "123!"
IE.document.getElementById("loginbutton").Click结束子对象
*下一个getElementById不能工作!我想这是因为在登录后有一个新的IE窗口弹出,而我的"IE“对象没有设置为使用它,那么我如何在我的"IE”对象上激活这个新会话呢?谢谢。
发布于 2017-11-17 01:11:53
Set objCollection = ie.Document.getElementsByTagName("input")
i = 0
Do While i < objCollection.length
Select Case objCollection(i).ID
Case "UserName"
Debug.Print "Found username field, setting value"
objCollection(i).Value = ufLoginDetails.txtUID
Case "Password"
Debug.Print "Found password field, setting value"
objCollection(i).Value = ufLoginDetails.txtPWD
ufLoginDetails.txtPWD = ""
Case "loginbutton"
Debug.Print
objCollection(i).Click
Exit Do
' i = objCollection.length
End Select
i = i + 1
Loop您必须验证按钮标签是输入还是按钮。
https://stackoverflow.com/questions/45085242
复制相似问题