i need to click on a 'button' where it does not have id , name it was a href into javascipt ..
1 Answer
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.
a.clickactually execute?