我试图仅选择包含“偏差”的href链接。我正在使用的当前VBA如下。
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
此代码选择第二个href链接,但是该链接并不总是第二个。在某些情况下,有4个href代码,并且位置也有所不同,而时间表班次ID也有所不同。有没有办法只单击链接中包含“偏差”的href?
<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>
答案 0 :(得分:1)
如果这是唯一包含该单词的链接,只需遍历所有a
标记名-您已经在使用它们-并获取innerText
属性。
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
答案 1 :(得分:0)
您可以尝试
ie.document.querySelector("[href*=deviation]").Click
以上答案取决于是否仅在内部文本为href
的元素的deviation
属性中也出现了偏差
这基于CSS属性=值组合,在其中查找具有href
属性且其值包含(*
)deviation
的第一个元素。
您可以通过加长字符串来使其更具体,以将href
中的字符串匹配到/spectrum/operations/deviation
:
ie.document.querySelector("[href*='/spectrum/operations/deviation']").Click
并添加一个类选择器,例如
ie.document.querySelector(".edit-schedules[href*='/spectrum/operations/deviation']").Click