2

Issues with Internet Explorer button using VBA.

I need help with clicking a button using vba. The issue is that the button is not clickable when I automatically put in the username and password. It is only clickable when I manually type in the username and password. My code works with other buttons on the website except for this one.

'Here is the part of my code that enters the username, password and supposed to click on the button.

    Set HTMLInput = HTMLDoc.getElementById("USER")
    HTMLInput.Value = "user"

    Set HTMLInput = HTMLDoc.getElementById("Password")
    HTMLInput.Value = "password"

    Set HTMLButton= HTMLDoc.getElementById("subBtn")
    HTMLButton.Click

'This is the button information

<button type="submit" class="btn btn-primary ng-scope" data-ng-click="saveUserID()" id="subBtn" translate="signin.signin">Sign In</button>

I've read somewhere that I can use the Application.sendkeys"username",True to simulate typing on the keyboard to see if the button will be clickable but I also cannot get that to work. Is there another way to make this button clickable?

QHarr
  • 83,427
  • 12
  • 54
  • 101
Chum Ba
  • 47
  • 6
  • There are a few ways to do this is when `.Click` doesn't work. The correct way depends on how the form is setup For example, you could fire the `onclick` event like [this](https://stackoverflow.com/a/39261308/8112776a) or you could `.submit()` the form itself liek [this](https://stackoverflow.com/a/28372169/8112776). – ashleedawg May 10 '19 at 18:26
  • Can you share the url? – QHarr May 10 '19 at 19:56

3 Answers3

1

Try to make the website think you're typing for real those values. For example:

Set HTMLInput = HTMLDoc.getElementById("USER")
HTMLInput.Focus '<-- set the focus on the input (make the element the active one)
Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 1) '<-- wait a second
HTMLInput.Value = "user"

Set HTMLInput = HTMLDoc.getElementById("Password")
HTMLInput.Focus '<-- set the focus on the input (make the element the active one)
Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 1) '<-- wait a second
HTMLInput.Value = "password"

Set HTMLButton= HTMLDoc.getElementById("subBtn")
HTMLButton.Click '<-- finally click the button

Note: since you didn't share the URL, this code is untested. But it has worked for me in the past under certain heavily JavaScript-based websites.

Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
1

Change the disabled attribute to False

Option Explicit
'VBE > Tools > References: Microsoft Internet Controls
Public Sub Login()
    Dim ie As New InternetExplorer
    With ie
        .Visible = True
        .Navigate2 "https://workforcenow.adp.com/workforcenow/login.html"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
            .querySelector("#user_id").Value = "something"
            .querySelector("#password").Value = "somethingElse"

            With .querySelector("#subBtn")
                .disabled = False
                .Click
            End With
            Stop '< delete me later
        End With

        While .Busy Or .readyState < 4: DoEvents: Wend
        'other stuff post login
        .Quit
    End With
End Sub
QHarr
  • 83,427
  • 12
  • 54
  • 101
  • 1
    I actually found how to do it using part of Matteo NNZ answer but since your answer also works I chose it as best answer. Thank you for taking your time to help me! – Chum Ba May 11 '19 at 02:48
0

This is how I solved it. Not sure if this is the best way to solve it but it works. The sendkeys command actually make it think that you're manually typing in the id and password. I needed to add the wait time because it skips on to the password quickly and doesn't finish entering the user_id. Thank you guys for taking your time to help me!

    HTMLInput.Focus '<-- set the focus on the input (make the element the active one)
    Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 1) '<-- wait a second
    SendKeys "user"
    Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 1) '<-- wait a second

    Set HTMLInput = HTMLDoc.getElementById("password")
    HTMLInput.Focus '<-- set the focus on the input (make the element the active one)
    Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 1) '<-- wait a second
    SendKeys "password"
    Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 1) '<-- wait a second

    Set HTMLbutton = HTMLDoc.getElementById("subBtn")
    HTMLbutton.Click '<-- finally click the button```
Chum Ba
  • 47
  • 6