简体   繁体   中英

VBA to click on a button in IE with no ID, Name, ClassName

I need to click on a button in IE. I tried few options with no success. Everything i tried are using TagName/ ClassName/ Name/ ID. But I don't find any of these in the below.

<FORM method=post name=frmSearch action=searchResultsSummary>
<INPUT type=hidden value=GMT+01:00 name=clitz> 
<DIV style="MARGIN-LEFT: 30px">
<DIV style="MARGIN-BOTTOM: 10px; FONT-WEIGHT: bold">Common Criteria</DIV>
<TABLE style="MARGIN-LEFT: 20px" cellSpacing=5 cellPadding=0 border=0>
<TBODY>
<TR>
<TD id=oDocumentCategoryCell style="HEIGHT: 22px" colSpan=3 noWrap>Document category:</TD>
<TD colSpan=3 align=left>
<SELECT style="DISPLAY: block" name=docCategory old_value="2">
<OPTION value=%>Any Category</OPTION>
<OPTION value=FINANCE>Finance Document</OPTION>
<OPTION selected value=INBOUND>Inbound Document</OPTION>
<OPTION value=PROCESS>Internal Process</OPTION>
<OPTION value=OUTBOUND>Outbound Document</OPTION>
</SELECT>
</TD>
<TD style="WIDTH: 15px"></TD>
<TD>
<INPUT onclick="javascript:if (!validateSearchForm()) return;if (!checkCountOfDays()) return; addRefKeysToForm(frmSearch); frmSearch.submit();" style="WIDTH: 100px" type=button value=Search>
</TD></TR>
<TR>
<TD style="HEIGHT: 22px" colSpan=3 noWrap>Document type:</TD>
<TD colSpan=3 align=left>
<SELECT style="DISPLAY: block" name=docType old_value="0">
<OPTION selected value=%>Any Type</OPTION>
<OPTION value=CSN_ORD_VALID>CSN Order Validation</OPTION>
<OPTION value=CREDIT_ORD>Credit Order</OPTION>
<OPTION value=EOI_ORDCPY>EOI Order Copy</OPTION>
<OPTION value=LEASE_ORD>Lease Order</OPTION>
<OPTION value=ORDER_REPORT>Order Load Report</OPTION>
<OPTION value=TRADE_ORD>Trade Order</OPTION>
<OPTION value=WATSON>WATSON Quote</OPTION>
<OPTION value=WATSON_UPDATE>WATSON Update Quote</OPTION>
<OPTION value=WNGQ>WNGQ Request</OPTION></SELECT> </TD></TR>
<TR>

I tried going by input tags and it is not working.

I used the below code. It clicks on the button i want but it is not considering the values i gave before clicking on submit button.

Dim btnInput As MSHTML.HTMLInputElement
Dim frm As MSHTML.IHTMLElementCollection
Application.ScreenUpdating = True

Set frm = ie.Document.getElementsByName("frmSearch")
For Each btnInput In frm

If btnInput.Value = "frmSearch.submit()" Then
    btnInput.submit
    Exit For
End If
Next btnInput

Can some one help me on how to click on the button.

As @Tim Williams suggest, this should work document.querySelector("input[value=Search]") .

If this still doesn't return the correct input element then the selector can be made more specific according to the DOM tree where the searched input is located, eg like this.

' Add reference to Microsoft Internet Controls (SHDocVw)
' Add reference to Microsoft HTML Object Library

Dim inputElement As HTMLInputElement
Set inputElement = doc.querySelector( _
    "div[class=main] div[id=inner] table input[type=button][value=Search]")

If Not inputElement Is Nothing Then
    inputElement.Click
End If

HTML

<div class="main">
    <div id="inner">
        <table>
            <tbody>
                <tr>
                    <td>
                        <INPUT onclick="alert('This is the corect one');" 
                            style="WIDTH: 100px" 
                            type=button 
                            value=Search>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
<INPUT onclick="alert('No this is not the searched one');" 
    style="WIDTH: 100px" 
    type=button 
    value=Search>

EDIT:

Not sure if I understand your problem correctly. To select the INPUT element this selector can be used:

Set inputElement = doc.querySelector( _
    "form[name=frmSearch] table tbody tr td input[type=button][value=Search]")

To set the value attribute setAttribute() function can be used. To check value attribute getAttribute() function can be used.

If Not inputElement Is Nothing Then
    inputElement.setAttribute "value", "some-new-value"
    If inputElement.getAttribute("value") = "some-new-value" Then
        inputElement.Click
    End If
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM