IE对象设置为活动IE窗口

时间:2017-07-13 15:32:11

标签: vba

我正在尝试使用访问IE的VBA宏登录网站, 一旦我到达这个URL并登录使用用户并传递然后我希望VBA中的IE对象使用弹出的新URL来操作它(插入到表单等...) 这是我的代码:

Sub Automate_IE_Load_Page() '这将在IE中加载网页     将IE视为对象     Dim URL As String

'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

End Sub

***下一个getElementById无法正常工作!我认为这是因为登录后有一个新的IE窗口弹出,我的" IE"对象未设置为使用它,如何使这个新会话"活动"在我的" IE"对象可以这么说? 感谢。

1 个答案:

答案 0 :(得分:0)

 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

您必须验证按钮标签是输入还是按钮。