1

I can't for the life of me figure out how to click the button below with VBA. Any help would be appreciated. I've been able to populate the username/password fields since they have a name so I use getElementsByName but the button doesn't have a name or ID.

The specific button code is:

<table class="button"><tr><td><div class="button-left"><input type="submit"  class="form-button"  value="Submit"  >

Here is the full script/form code in case it helps

<form name="loginFormBean" method="post" action="/XXXXXXXX/login.do" onsubmit="return validateForm(this)">
    <TABLE border="0" cellpadding="5" cellspacing="0">
        <TR>
            <TD class="bigGreyContent" nowrap>User ID</TD>
            <TD class="bigGreyContent" align="left">
                <input type="text" name="username" maxlength="50" size="40" value="" class="bgGreenColor">
            </TD>
        </TR>
        <TR>
            <TD class="bigGreyContent" nowrap>Password</TD>
            <TD class="bigGreyContent" align="left">
                <input type="password" name="password" maxlength="50" size="40" value="" class="bgGreenColor">
            </TD>
        </TR>
        <TR>
            <TD colspan="2" align="center">
            <BR>
            <table class="button"><tr><td><div class="button-left"><input type="submit"  class="form-button"  value="Submit"  ></div><div class="button-right"></div></td></tr></table>     
            </TD>
        </TR>
    </TABLE>
</form>

EDIT: I have the following references enabled (in addition to the default ones):

Microsoft HTML Object Library Microsoft Internet Controls Microsoft WinHTTP Services, Version 5.1 Microsoft XML, v6.0

My current code is:

Sub XXXX()

    Dim objIE As InternetExplorer
    Dim http As New MSXML2.XMLHTTP60
    Dim html As New HTMLDocument
    Dim btn As Object
    Set objIE = New InternetExplorer
    Set btn = html.getElementsByClassName("button-left")(0).getElementsByTagName("input")(0)
    objIE.Visible = True
    objIE.navigate "http://XXXX/login.jsp"
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
    objIE.document.getElementsByName("username")(0).Value = "XXXX"
    objIE.document.getElementsByName("password")(0).Value = "YYYY"
    btn.Click

End Sub

2 Answers 2

3

Assuming class="button-left" is the first occurrence within your html, try this:

Dim btn As Object
Set btn = html.getElementsByClassName("button-left")(0).getElementsByTagName("input")(0)
btn.Click

Edit:

You should add Microsoft HTML Object Library and Microsoft XML v6.0 if you haven't already, from Tools -> References.

Currently you are stuck between your method and my method. You need to pick one so please try this:

Sub XXXX()
Dim http As New MSXML2.XMLHTTP60
Dim html As New HTMLDocument
Dim btn, usr, psw As Object

With CreateObject("MSXML2.serverXMLHTTP")
    .Open "GET", "http://XXXX/login.jsp", False
    .send
    html.body.innerHTML = .responseText
End With

Set usr = html.getElementsByTagName("Form")(0).getElementsByTagName("input")(0)
Set psw = html.getElementsByTagName("Form")(0).getElementsByTagName("input")(1)
Set btn = html.getElementsByTagName("Form")(0).getElementsByTagName("input")(2)

usr.Value = "Username"
psw.Value = "Password"
btn.click

Set html = Nothing: Set btn = Nothing: Set usr = Nothing: Set psw = Nothing
End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply, however I'm getting a 424/Object Required error on the line beginning with 'Set btn', any thoughts? From what I can tell, this IS the first instance of button-left.
Make sure you have the following in your code: Dim http As New MSXML2.XMLHTTP60 Dim html As New HTMLDocument Dim btn As Object. If it still doesn't work, please share your code, so I can stop guessing what the problem is.
I've added the code and other info above. Now that I added the two additional Dim's you provided, I get a 91 / Object variable or with block variable not set error, on the same line as before.
0

Your code is almost corect.You need to set Set html = objIE.document and then change the position of Set btn = html.getElementsByClassName(...) so it is after the document has been loaded. Btw. the answer from @Tehscript works as well.

Dim objIE As InternetExplorer
Dim http As New MSXML2.XMLHTTP60
Dim html As New HTMLDocument
Dim btn As Object
Set objIE = New InternetExplorer

objIE.Visible = True
objIE.navigate Path
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Set html = objIE.document ' <-- This is the cause of the error 91
Set btn = html.getElementsByClassName("button-left")(0).getElementsByTagName("input")(0) ' <-- Button can be set after the document has been loaded
objIE.document.getElementsByName("username")(0).Value = "XXXX"
objIE.document.getElementsByName("password")(0).Value = "YYYY"
btn.Click

Comments

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.