0

i need to click on a 'button' where it does not have id , name it was a href into javascipt ..

7
  • Try execScript("callSearch();")? Not sure it would work, just seems like correct syntax. Thats some horrible html by the way. Commented Sep 21, 2019 at 14:58
  • is there an url to use? Commented Sep 21, 2019 at 15:27
  • Hi Lorne, indeed it was horrible... hi QHarr. sorry cant share the URL because it's intranet. but that's almost the entire html code for that page Commented Sep 21, 2019 at 15:42
  • In "try1" does a.click actually execute? Commented Sep 21, 2019 at 20:03
  • @Tim. able to find the element, execute the click , but not responses Commented Sep 22, 2019 at 2:41

1 Answer 1

0

Try to use the following code, it seems that when we click the link, it will fire the JavaScript function:

Sub Main()
    'we define the essential variables

    Dim IE As Object, Data As Object
    Dim ticket As String


    Set IE = CreateObject("InternetExplorer.Application")

    With IE
        .Visible = True
        .navigate ("<the website url>")

        While IE.ReadyState <> 4
            DoEvents
        Wend

        Set Data = IE.Document.getElementsByClassName("appbut")

        Debug.Print Data.Length

        If Len(Data) > 0 Then
            For Each ee In Data

                Set link = ee.getElementsbyTagName("a")(0)
                ' it is better to check whether we could find the a tag.
                'check whether we could get the innertext.
                Debug.Print link.InnerText

                If link.InnerText Like "Cancel" Then

                    'click the link.
                    link.Click

                End If
            Next ee

        End If
    End With
    Set IE = Nothing
End Sub

The code in the web page:

<script>
    function callLookup() {
        alert("Party Search");
    };
    function callCancel() {
        alert("Cancel");
    };
    function callSearch() {
        alert("Search");
    }
</script>
<table  >
    <tr valign="bottom">
        <td colspan="5">
            <table  >
                <tbody>
                    <tr>
                        <td class="butspace"></td>
                        <td class="butspace"></td>
                        <td class="butspace"></td>
                        <td class="butspace"></td>
                        <td class="butspace"></td>
                        <td class="butspace"></td>
                        <td class="butspace"></td>

                        <td class="butspace"></td>
                        <td class="appbut"><a href="javascript:callLookup();">Party Search</a></td>
                    </tr>
                    <tr>
                        <td class="appbut"><a href="javascript:callCancel()">Cancel</a></td>
                        <td class="butspace"></td>
                        <td class="appbut"><a href="javascript:callSearch();">Search</a></td>
                        <td></td>
                        <td></td>
                    </tr>
                </tbody>
            </table>
        </td>
    </tr>

</table>

The result like this.

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

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.