VBA to click on button

徘徊边缘 提交于 2019-12-23 22:05:26

问题


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?


回答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



回答2:


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.




回答3:


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```


来源:https://stackoverflow.com/questions/56082906/vba-to-click-on-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!