4

I load an internet page and log in with myusername and mypassword using VBA.

I would like to write a search-term in an input field of the webpage using VBA.

Because there is no name and ID in the input field, I didn't succeed.

Here is my code :

Sub MyLogin()

Dim IE As InternetExplorer
Set IE = CreateObject("InternetExplorer.Application")
With IE
    .Visible = True
    .navigate "https://www.example.com"

    Do Until .readyState = 4
        DoEvents
    Loop

    .document.all.Item("username").Value = "myusername"
    .document.all.Item("password").Value = "mypassword"
    .document.forms(0).submit

End With

Do Until IE.readyState = 4
    DoEvents
Loop

Dim inputfield As HTMLInputElement
For Each inputfield In IE.document.getElementsByTagName("input")
    If inputfield.Type = "text" Then inputfield.Value = "mysearch"
Next

Do Until IE.readyState = 4
    DoEvents
Loop

End Sub

The HTML field I want to fill:

input class="input-inboxsearch form-control" type="text" value="" placeholder="Nom, email" maxlength="71"

How can I set the value of the input field?

1
  • 2
    Try using getElementsByClassName ("input-inboxsearch form-control") instead, Commented Aug 6, 2016 at 13:31

1 Answer 1

1

Further to @Miqi180's comment, you can try replacing your the section of your code starting with Dim inputfield As HTMLInputElement with this:

Dim inputfield As Object
Set inputfield = IE.document.getElementsByClassName("input-inboxsearch form-control")
If inputfield.length > 0 Then
    inputfield(0).Value = "mysearch"
End If

'carry on with your code

The getElementsByClassName method will return a collection so you have to check its length and then access the items of the collection. If there is only one element with that class then you can reference the 0th element of the collection.

I've set the inputfield variable as an Object because you appear to be using late binding.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your help. I did the modifications. There is no error message. However nothing appear un the search field.
When I debug.print inputfield.length, it returns 0. Is there something which can prevent the getelementsbyclass process to collect data? When I try the process on a google webpage, it works perfectly fine. Thanks again for your help.
Maybe you need to double-check the class attribute on the input field - are you sure it is input-inboxsearch form-control ? Normally, this should just work - I note that your code already has navigated to a page and then logged in. Maybe you need to check that IE.document refers to your second page (after log-in) not the first (pre log-in).
Many thanks for your assist. The class attribute is correct. I think there is a conflict between the 2 webpages as you said (pre log-in and after log-in). Should I rename the second webpage ? Any advise on how to do it would be very appreciated. The help provided so far has been extremely useful. Thanks to the contributers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.