[英]VBA - How to select HTML href link containing a word
I am attempting to select only the href link that contains "Deviation." 我试图仅选择包含“偏差”的href链接。 The current VBA i am using is as follows. 我正在使用的当前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
This code selects the second href link, however the link is not always the second one. 此代码选择第二个href链接,但是该链接并不总是第二个。 There are some instances that there are 4 href codes and the position varies, as well as the schedule shift ID varies. 在某些情况下,有4个href代码,并且位置也有所不同,而时间表班次ID也有所不同。 Is there a way to click just the href containing "deviation" in the link? 有没有一种方法可以只单击链接中包含“偏差”的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>
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. 如果这是包含该单词的唯一链接,则只需遍历所有已经在做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
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
上面的答案取决于偏差是否也仅出现在具有内部文本deviation
的元素的href
属性中
This is based on a CSS attribute = value combination where it looks for first element with href
attribute having value containing ( *
) deviation
. 这基于CSS属性=值组合,在其中查找具有href
属性具有包含( *
) deviation
值的第一个元素。
You could make more specific by adding in lengthening the string to match on in the href
to /spectrum/operations/deviation
: 您可以通过加长字符串来使其更具体,以在/spectrum/operations/deviation
的href
进行匹配:
ie.document.querySelector("[href*='/spectrum/operations/deviation']").Click
and adding a class selector eg 并添加一个类选择器,例如
ie.document.querySelector(".edit-schedules[href*='/spectrum/operations/deviation']").Click
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.