1

我一直在尝试将用户名和密码输入到远程访问的 GUI 中。

这是我的代码:

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

我遇到的问题是,当我单步执行代码(F8)以观察每一行的执行时,我注意到当它到达 For Each Next 循环时,它会一起跳过循环。我知道用户名和密码文本框嵌入在 html 代码的表格中。

输入元素朝向 html 代码块 GUI HTML 代码的底部...

<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>

似乎Set Webdoc = IE.Document.getElementsByTagName("input")代码没有“看到”表中的输入标签。如果我退出并尝试调用表格之外的任何内容,则代码将完美运行。

任何人都可以帮我找出造成这种情况的原因。我愿意回答任何其他问题。提前感谢您提供的任何帮助!

4

0 回答 0