访问Web表输入元素

时间:2016-09-26 20:00:43

标签: html vba excel-vba html-table htmltable-control

我一直在尝试将用户名和密码输入到我远程访问的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")上代码没有“看到”表中的输入标记。如果我退出并尝试调用表外的任何内容,代码就会完美无缺。

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

0 个答案:

没有答案