Reputation: 533
I have the following code to fill a webform, but in this particular website the code is not working:
Const loginSelectSistema_Name = "url"
Const loginInputUsuário_Name = "Usuario"
Const loginInputSenha_Name = "Senha"
Const loginInputOk_Name = "imageField2"
Const USERNAME = "NomeUsuário"
Const PASSWORD = "SenhaAcesso"
Dim ieApp As InternetExplorer
Dim iePage As HTMLDocument
Function loginPage()
'
Set ieApp = New InternetExplorer
'
ieApp.Visible = True
ieApp.navigate (loginWebsite)
'
'Wait for page to load
Do Until ieApp.ReadyState = READYSTATE_COMPLETE
Loop
'
'
Set iePage = ieApp.Document
'
fillInputObject loginSelectSistema_Name, "jcaa"
fillInputObject loginInputUsuário_Name, USERNAME
fillInputObject loginInputSenha_Name, PASSWORD
'
iePage.all.Item(loginInputOk_Name).Click
End Function
Private Sub fillInputObject(ByVal objectName As String, ByVal value As String, Optional IdentificadorElemento As IdentificadorElementoHTML = Nome)
'
'Preenche o Objeto Input com um determinado valor
Dim elementHTML As HTMLObjectElement
'
If (IdentificadorElemento = Nome) Then
Set elementHTML = iePage.getElementsByName(objectName).Item
End If
'
If (IdentificadorElemento = Id) Then
Set elementHTML = iePage.getElementById(objectName)
End If
'
If (IsObject(elementHTML)) Then
If (Not (elementHTML Is Nothing)) Then
elementHTML.value = value
End If
End If
End Sub
When debugging, in Function fillInputObject
the line: If (Not (elementHTML Is Nothing)) Then
the value is always Nothing, and the webform is not filled.
Below is the page in HTML that I trying to fill the webform:
<table width="90%" cellspacing="0" cellpadding="0" bordercolor="#DADADA" border="1" bgcolor="#F7F7F8" align="center" height="110">
<tbody><tr height="2"><td width="62"></td><td width="0"></td><td width="592"></td><td width="223"></td></tr>
<tr height="15">
<td colspan="4" bordercolor="#F7F7F8" style="vertical-align:left"><strong>Selecione o Sistema:</strong></td>
</tr>
<tr height="15">
<td colspan="4" bordercolor="#F7F7F8" style="vertical-align:left">
<font size="1px" face="Verdana, Arial, Helvetica, sans-serif">
<select name="url" class="combobox" style="width:180px;">
<option value="jcaa">Cadastro de Alunos</option>
<option value="paef">Cadastro Funcional</option>
<option value="paec">Controle de Freqüência</option>
<option value="papc">Desp de Pessoal (SDPE)</option>
<option value="jcgo">Cargos</option>
<option value="paea">Contagem de Tempo</option>
<option value="jata">Formação Curricular</option>
<option value="jrha">Gratificação / Promoção QM</option>
<option value="paex">PAEX - Cadastro Funcional</option>
</select>
</font>
</td>
</tr>
<tr height="2"><td colspan="4" bordercolor="#F7F7F8"></td></tr>
<tr><td colspan="2" bordercolor="#F7F7F8" align="left">
<font size="1" face="Verdana, Arial, Helvetica, sans-serif">
Usuário
</font>
</td>
<td colspan="2" bordercolor="#F7F7F8" align="left">
<input id="Usuario" name="Usuario" size="10" maxlength="10" value="SE0452336A" type="text">
</td></tr>
<tr><td colspan="2" bordercolor="#F7F7F8" align="left">
<font size="1" face="Verdana, Arial, Helvetica, sans-serif">
Senha
</font>
</td>
<td colspan="2" bordercolor="#F7F7F8" align="left">
<input id="Senha" name="Senha" size="14" maxlength="14" type="password">
</td></tr>
<tr>
<td colspan="2" bordercolor="#F7F7F8" align="left">
<font size="1" face="Verdana, Arial, Helvetica, sans-serif">
</font>
</td>
<td colspan="2" bordercolor="#F7F7F8" align="left">
<input name="imageField2" src="images/ok_sen.gif" width="24" type="image" border="0" height="17">
</td></tr>
</tbody></table>
What am I doing wrong?
Upvotes: 1
Views: 56
Reputation: 14053
The URL provided in the last comment shows the problem. The login form is placed inside of an IFrame
therefore the current document doesn't know about the elements because they are not there.
You should slightly modify your code so the document of the IFrame
is used for search.
Set iePage = ieApp.document
Dim fr As HTMLIFrame
Set fr = iePage.frames("aplicativos")
Dim iframeDoc As HTMLDocument
Set iframeDoc = iePage.frames("aplicativos").document
fillInputObject iframeDoc, loginSelectSistema_Name, "jcaa"
fillInputObject iframeDoc, loginInputUsuário_Name, USERNAME
fillInputObject iframeDoc, loginInputSenha_Name, PASSWORD
iframeDoc.all.Item(loginInputOk_Name).Click
And modify the function fillInputObject
so it uses the right Document
object.
Private Sub fillInputObject(doc As HTMLDocument, ByVal objectName As String, ByVal Value As String, Optional IdentificadorElemento As IdentificadorElementoHTML = Nome)
'
'Preenche o Objeto Input com um determinado valor
Dim elementHTML As HTMLObjectElement
'
If (IdentificadorElemento = Nome) Then
Set elementHTML = doc.getElementsByName(objectName).Item
End If
'
If (IdentificadorElemento = ID) Then
Set elementHTML = doc.getElementById(objectName)
End If
'
If (IsObject(elementHTML)) Then
If (Not (elementHTML Is Nothing)) Then
elementHTML.Value = Value
End If
End If
End Sub
Upvotes: 1