1

I have been attempting to input a username and password into a GUI that I am accessing remotely.

Here is my Code:

Dim UNBox As IHTMLInputElement
Dim PWBox As HTMLInputElement
Dim Login As HTMLInputElement
Dim doc As HTMLDocument
Dim LoginInfo As String
Dim UN As String
Dim PSW As String
Dim Webdoc As MSHTML.IHTMLElementCollection
Dim Element As MSHTML.IHTMLInputElement
Dim IE As Object


LoginInfo = CmbNetwork.Value

'launches IE and inputs IP address into the address bar then navigates to the GUI
    Set IE = CreateObject("InternetExplorer.application")
    IE.Visible = True
    IE.navigate ThisWorkbook.Worksheets("Networks Info").Cells.Find(LoginInfo).Offset(rowoffset:=0, columnoffset:=8).Value
    IE.Top = 0
    IE.Left = 150
    IE.Height = 700
    IE.Width = 1000

'Pauses browser window while the GUI loads
Do While IE.readyState <> READYSTATE_COMPLETE
     Application.Wait (Now + TimeValue("00:00:05"))
Loop

    UN = ThisWorkbook.Worksheets("Networks Info").Cells.Find(LoginInfo).Offset(rowoffset:=0, columnoffset:=13)
    PSW = ThisWorkbook.Worksheets("Networks Info").Cells.Find(LoginInfo).Offset(rowoffset:=0, columnoffset:=14)

If ThisWorkbook.Worksheets("Networks Info").Cells.Find(LoginInfo).Offset(rowoffset:=0, columnoffset:=12) = "ION-M" Then

Set Webdoc = IE.Document.getElementsByTagName("input")

'Loops through all elements in GUI
For Each Element In Webdoc 'READS THIS LINE

    'Finds username text box element and inputs username
    If Element.Name = "loginname" Then
        Element.innerText = UN

    'Finds password text box element and inputs password
    ElseIf Element.Name = "loginpwrd" Then
        Element.innerText = PSW

    End If

Next Element

Set Element = Nothing 'SKIPS TO THIS LINE

The issue that I am having is that when I step through the code(F8) to watch each line execute I have noticed that when it hits the For Each Next loop it skips the loop all together. I know that the username and password text boxes are embedded in a table in the html code.

The input elements are toward the bottom of the html code block GUI HTML code...

<form name="myLogin" onsubmit="return setTimeDiff();" action="index.jsp" method="post">
<input name="loginTimeDiff" type="hidden" value="0"><table border="0">
<tbody>
<tr>
<td align="right">User Name:</td>
<td>
<input name="login" type="hidden" value="login">
<input name="loginname" id="login_name" type="text" size="20" maxlength="20">
</td>
</tr><tr>
<td align="right">Password:</td>
<td>
<input name="loginpwrd" onkeyup="zaehlen(this.value)" type="password" size="20" maxlength="8">
</td>
</tr>
<tr height="25"></tr><tr><td align="center" colspan="2">
<input type="submit" value=" Login "><input type="reset" value=" Reset ">
</td>

It seems that on the line Set Webdoc = IE.Document.getElementsByTagName("input") that the code does not "see" the input tags in the table. If I back out and try to call anything outside of the tables the code works perfect.

Can anyone please help me to discover what is causing this. I am willing to answer any additional questions. Thank you in advance for any help you can provide!

Joe
  • 11
  • 4
  • Try it as `Element.Value = UN` Same for `PSW`. –  Sep 26 '16 at 20:04
  • Isn't the input tag supposed to be self-closed like `` not with a closing tag? –  Sep 26 '16 at 20:07
  • Also, you should also check to make sure that IE isn't busy. Consider updating the while statement to include `Do While IE.readyState <> READYSTATE_COMPLETE or IE.Busy`. It may be skipping over the `For Each` because there hasn't been enough time (or is looking at all relevant properties) for the page to load fully. Last thought, is simply declare the `Element` as an `Object`. – Ryan Wildry Sep 26 '16 at 20:12
  • It never drops into the for each loop so `Element.innertext = UN` never even gets looked at. – Joe Sep 26 '16 at 20:12
  • I have tried it as an object but that does not work either – Joe Sep 26 '16 at 20:18
  • Are you sure that the page has fully loaded? How many Input tags are showing up? You can print out `Webdoc.Length` to see how many Input tags were found. – Ryan Wildry Sep 26 '16 at 20:21
  • Jeeped I have never noticed that the input was not closed before but the code works and was not made by me. I have no idea if that matters as I am not any good whatsoever with HTML. – Joe Sep 26 '16 at 20:25
  • Ryan, the length returned 0 input tags. As I said above it does not see them for some reason. I also know that the site is fully loaded when it is executing that particular block of code because I placed a Toggle Break at `Set Webdoc = IE.Document.getElementsByTagName("input")` and then manually stepped through the code. – Joe Sep 26 '16 at 20:31
  • Try to make 3-step checks like in [this answer upd4](http://stackoverflow.com/a/23232573/2165759), although I'm not sure since you said that the site is fully loaded when it is executing that particular block of code. Are there any frames in document? Could you post the entire webpage HTML content, or share the URL? – omegastripes Sep 26 '16 at 20:47
  • I have added the html code – Joe Sep 26 '16 at 21:17
  • Try to open browser developer tools (F12), and type in console the same command in JS `document.getElementsByTagName("input")` (case-sensitive), check if collection isn't empty. – omegastripes Sep 27 '16 at 10:02
  • 1
    I did as you suggested and it looks as if all 6 input elements on the page were in the collection. I have since tried adding a do loop saying `if IE.documents.getElementsByTagName("input").Length <> 0 Then Exit Loop` but the length never changes in my code once the page is loaded? – Joe Sep 27 '16 at 15:54
  • Try to execute VBA code `Set cNodes = IE.Document.getElementsByTagName("*"): MsgBox cNodes.Length`. Does the collection contain any nodes? Update the question with info from the comments. – omegastripes Sep 27 '16 at 18:07
  • It returns a value of 4 – Joe Sep 27 '16 at 18:11
  • Add loop to find out what are the nodes there, like `For Each oNode In cNodes: Debug.Print oNode.tagName: Next`. – omegastripes Sep 27 '16 at 18:26
  • This returns HTML, HEAD, META, BODY. – Joe Sep 27 '16 at 19:16
  • One other thing I realized that I have not shared with you yet. This code works sometimes (Maybe 10% of the time). When I run the `For Each oNode`, as you asked me to above, the code sometimes returns 41 tags. When this happens there is no META tag included within the 41 tags as it is when the `For Each` loop only returns 4 tags. If the 41 nodes do show up then the inputs that my code is looking for are in those nodes and the code works... Just thought I would give you that information in the hopes that it will help. Thank you again for all of the help you have provided so far! – Joe Sep 27 '16 at 20:48
  • That's a weird buggy behaviour. Please edit your question and add more detailed info. What is the webpage URL? Is it in Internet or Intranet / Local network? Is it in trusted sites in IE settings? Is IE protected mode enabled? What is the versions and bitness of Windows and IE you running on? If you are on 64-bit OS, then find the launched processes `wscript.exe` and `iexplore.exe` in task manager, check if they have `*32` mark or not (if there are several `iexplore.exe`, display command line column in task manager to find out which is the process). – omegastripes Sep 28 '16 at 20:39
  • I have told my code to call all of the elements as the first task after the IE is ready and this seems to have rectified the problem. I am going to send it to some of my colleagues and have them run it on their machines as well. I will report back within the next week if the issue is rectified and post the code that I used. Again, Thank you for all of your help! – Joe Sep 29 '16 at 14:50

0 Answers0