-1

I'm trying to use VBA to login on site at work, with this code that I found on web.

Sub LoginSite()
 Dim IE As Object
 Set IE = CreateObject("InternetExplorer.application")
 With IE
  .Visible = True
  .Navigate ("``m``y site")
 While .Busy Or .ReadyState <> 4: DoEvents: Wend
  .Document.getElementById("look the Login ID at site").Focus
  .Document.getElementById("look the Login ID at site ").Value = "my user"
  .Document.getElementById("look the Password ID at site").Focus
  .Document.getElementById("look the Password ID at site").Value = "my password"
  .Document.All("Verificar qual ID do Botão ").Click
 While .Busy Or .ReadyState <> 4: DoEvents: Wend
Debug.Print .LocationURL
End With
End Sub

But when I look at codes of the site, there isn't ID names for Login or Password. As posted at links bellow

https://pixeldrain.com/u/yyswM5j8

https://pixeldrain.com/u/oxNJ8_Pi

https://pixeldrain.com/u/hfteWoWY

LOGIN: <INPUT style="HEIGHT: 22px; WIDTH: 102px" maxLength=11 name=Usuario>

PASSWORD: <INPUT style="HEIGHT: 22px; WIDTH: 102px" maxLength=10 type=password value="" name=Senha>

There is another way to fill the login and password without the ID name??

Edit1: If I fill the username field or password field, results this

https://pixeldrain.com/u/dB-IB9gR

borjaille
  • 1
  • 1
  • I can't quickly find a decent dupe target but this is a dupe. You can use attribute selectors e.g. ie.document.querySelector("[name=Usuario]").value = "username" ....... – QHarr Nov 20 '19 at 18:22
  • If I fill the username field or password field, results this https://pixeldrain.com/u/dB-IB9gR – borjaille Nov 20 '19 at 22:19

1 Answers1

0

You could find the html elements based on the elements' Name attribute, try to use the getElementsByName() method or the querySelector() method.

Sample code as below:

Sub Test()
    Dim IE As Object

    Dim startDateText As Object, endDateText As Object

    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Visible = True
        .Navigate "<web page url>"

        While IE.ReadyState <> 4
            DoEvents
        Wend
        ''uncomment the following code to use the getElementsByName() method to find the elements.
        '.document.getElementsByName("Usuario")(0).Focus
        '.document.getElementsByName("Usuario")(0).Value = "rodrigo.bs"
        '.document.getElementsByName("Senha")(0).Focus
        '.document.getElementsByName("Senha")(0).Value = "Rb123456"
        '.document.getElementsByName("Submit")(0).Click

        'using the queryselector method to find the elements (based on the input element name attribute). 
        IE.document.querySelector("input[name='Usuario']").Focus
        IE.document.querySelector("input[name='Usuario']").Value = "rodrigo.ba"
        IE.document.querySelector("input[name='Senha']").Focus
        IE.document.querySelector("input[name='Senha']").Value = "Rb123456"
        IE.document.querySelector("input[name='Submit']").Click

    End With
    Set IE = Nothing
End Sub

the web page resource:

<table>
    <tr>
        <td>
            Usuario:
        </td>
        <td>
            <input name="Usuario" id="" style="height:22px; width:102px" type="text" maxlength="11" value="" />
        </td>
    </tr>
    <tr>
        <td>
            Senha:
        </td>
        <td>
            <input name="Senha" id="" style="height:22px; width:102px" type="password" maxlength="10" value="" />
        </td>
    </tr>
    <tr> 
        <td colspan="2">
            <input name="Submit" id="" type="button" value="Submit" onclick="javascript: alert(document.getElementsByName('Usuario')[0].value + ' - ' + document.getElementsByName('Senha')[0].value);" />
        </td>
    </tr>
</table>

The output like this:

enter image description here

Zhi Lv
  • 18,845
  • 1
  • 19
  • 30
  • An error occurred in VBA while using your code. Maybe because the "value" in developer mode doesn't appear until I fill in the field https://pixeldrain.com/u/BQ1_BZa1 https://pixeldrain.com/u/tYX9_eID – borjaille Nov 22 '19 at 16:36
  • How about using the getElementsByName method, whether it works well? Besides, about the "Method 'Document' of object 'IWebBrowser2' failed" error, try to use internet explorer at a medium integrity level. InternetExplorer defaults to a low integrity level which, if you are doing this over a local intranet at work, sometimes will give the second error message you show above. More detail information, please refer [this thread](https://stackoverflow.com/questions/30086425/excel-vba-method-document-of-object-iwebbrowser2-failed). – Zhi Lv Nov 25 '19 at 01:50