Yulian
Yulian

Reputation: 76

IE object set to active IE window

I am trying to login to a website using VBA macro that accesses IE, once I get to this URL and am login in using user and pass and then I want the IE object in VBA to use this new URL of the pop up to be able to manipulate it (insert to form etc..) This is my code:

Sub Automate_IE_Load_Page() 'This will load a webpage in IE Dim IE As Object 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

*** The next getElementById wont work! I think this is becasue right after login there is a new IE window that pops up and that my "IE" object is not set to use it, how do I make this new session "active" on my "IE" object so to speak? thanks.

Upvotes: 0

Views: 1415

Answers (1)

J diaz
J diaz

Reputation: 1

 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

You have to validate whether the button label is an input or button.

Upvotes: 0

Related Questions