![](/img/trans.png)
[英]How do I test if a HTML link element (a) containing a link href exists at DOM?
[英]VBA - How to select HTML href link containing a word
我試圖僅選擇包含“偏差”的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>
如果這是包含該單詞的唯一鏈接,則只需遍歷所有已經在做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
你可以試試
ie.document.querySelector("[href*=deviation]").Click
上面的答案取決於偏差是否也僅出現在具有內部文本deviation
的元素的href
屬性中
這基於CSS屬性=值組合,在其中查找具有href
屬性具有包含( *
) deviation
值的第一個元素。
您可以通過加長字符串來使其更具體,以在/spectrum/operations/deviation
的href
進行匹配:
ie.document.querySelector("[href*='/spectrum/operations/deviation']").Click
並添加一個類選擇器,例如
ie.document.querySelector(".edit-schedules[href*='/spectrum/operations/deviation']").Click
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.