简体   繁体   中英

VBA - How to select HTML href link containing a word

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">&nbsp;</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">&nbsp; 
      </td>
      <td class="detail-cell scheduled-interval-cell whiteText" id="E1">    
         <a class="edit-schedules" href="/spectrum/operations/scheduledBreakCreateDisplay.do?method=createBreakDisplay&amp;scheduledShiftId=221749432">Break</a>
      </td>
      <td class="detail-cell scheduled-interval-cell whiteText" id="F1">&nbsp;</td>
      <td class="detail-cell scheduled-interval-cell whiteText" id="G1">
         <a class="edit-schedules" href="/spectrum/operations/deviationCreateDisplay.do?method=createNewDeviation&amp;scheduledShiftId=221749432">Deviation</a>
      </td>
   </tr>

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

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 eg

ie.document.querySelector(".edit-schedules[href*='/spectrum/operations/deviation']").Click

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