VBA-如何选择包含单词的HTML href链接
我试图只选择包含“偏差”的href链接。我正在使用的当前VBA如下所示VBA-如何选择包含单词的HTML href链接,html,excel,vba,web-scraping,Html,Excel,Vba,Web Scraping,我试图只选择包含“偏差”的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 Objec
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
Sub-code_Spectrum()
Dim URL作为字符串,UN作为字符串,PW作为字符串
模糊的物体
Dim HTMLDoc作为对象
作为对象的Dim objC
作为对象的暗淡元素
尺寸l为长,sel为对象,x为长,str为字符串
将e作为对象
作为整数的Dim t
用IE文件
对于IE.document.getelementsbytagname(“a”)中的每个e
如果e.classname=“编辑计划”,则
如果t<1,则
t=t+1
后藤3号线
其他的
e、 点击
退出
如果结束
如果结束
第3行:
下一个e
以
此代码选择第二个href链接,但该链接并不总是第二个。在某些情况下,有4个href代码,位置不同,并且计划班次ID也不同。有没有办法只点击链接中包含“偏差”的href
仅限UHCME
周一上午7:00-周一下午3:30
如果这是包含该单词的唯一链接,只需在所有的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
上述答案取决于偏差是否仅出现在带有内部文本的元素的href
属性中
这是基于CSS attribute=value组合,其中它查找具有href
属性的第一个元素,该属性的值包含(*
)偏差
您可以通过在href
中添加加长字符串以匹配/spectrum/operations/developeration
,使其更加具体:
ie.document.querySelector("[href*='/spectrum/operations/deviation']").Click
以及添加一个类选择器,例如
ie.document.querySelector(".edit-schedules[href*='/spectrum/operations/deviation']").Click