Reputation: 3
I am attempting to select only the href link that contains "Deviation." The current VBA i am using is as follows.
Sub Code_Spectrum()
Dim URL As String, UN As String, PW As String
Dim IE As Object
Dim HTMLDoc As Object
Dim objC As Object
Dim elems As Object
Dim l As Long, sel As Object, x As Long, str As String
Dim e As Object
Dim t As Integer
With IE.document
For Each e In IE.document.getelementsbytagname("a")
If e.classname = "edit-schedules" Then
If t < 1 Then
t = t + 1
GoTo Line3
Else
e.Click
Exit For
End If
End If
Line3:
Next e
End With
This code selects the second href link, however the link is not always the second one. There are some instances that there are 4 href codes and the position varies, as well as the schedule shift ID varies. Is there a way to click just the href containing "deviation" in the link?
<table width="100%" class="schedule-detail-grid" id="grid1" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
</tr>
<tr>
<td class="detail-cell scheduled-interval-cell whiteText sp-left-edge" id="A1"> </td>
<td class="detail-cell scheduled-interval-cell whiteText" id="B1">
UHCMAE - Only
</td>
<td class="detail-cell scheduled-interval-cell whiteText" id="C1">
Mon 7:00 AM - Mon 3:30 PM
</td>
<td class="detail-cell scheduled-interval-cell whiteText" id="D1">
</td>
<td class="detail-cell scheduled-interval-cell whiteText" id="E1">
<a class="edit-schedules" href="/spectrum/operations/scheduledBreakCreateDisplay.do?method=createBreakDisplay&scheduledShiftId=221749432">Break</a>
</td>
<td class="detail-cell scheduled-interval-cell whiteText" id="F1"> </td>
<td class="detail-cell scheduled-interval-cell whiteText" id="G1">
<a class="edit-schedules" href="/spectrum/operations/deviationCreateDisplay.do?method=createNewDeviation&scheduledShiftId=221749432">Deviation</a>
</td>
</tr>
Upvotes: 0
Views: 1311
Reputation: 84465
You could try
ie.document.querySelector("[href*=deviation]").Click
The above answer depends on whether deviation only also appears in the href
attribute for the element(s) with inner text deviation
This is based on a CSS attribute = value combination where it looks for first element with href
attribute having value containing (*
) deviation
.
You could make more specific by adding in lengthening the string to match on in the href
to /spectrum/operations/deviation
:
ie.document.querySelector("[href*='/spectrum/operations/deviation']").Click
and adding a class selector e.g.
ie.document.querySelector(".edit-schedules[href*='/spectrum/operations/deviation']").Click
Upvotes: 1
Reputation: 10139
If that's the only link containing that word, just loop through all the a
tagnames - which you are already doing - and grab the innerText
property.
Dim aTag As Object
For Each aTag In ie.document.getelementsbytagname("a")
If aTag.innertext = "Deviation" Then
aTag.Click
Exit For
End If
Next aTag
Upvotes: 1