我正在尝试创建一个简单的Excel VBA Web抓取宏。有一次,我需要宏来点击一个按钮(在封闭的图像下面 - button class="edit"
),它位于特定的class="title">TitleName
内。在网页上,有多个TitleName
,我始终需要转到特定的TitleName
,然后点击TitleName
特定的修改按钮。
答案 0 :(得分:1)
这应该适合你。
抓取button
的tagName集合,然后迭代它们,直到找到className为edit
的那个。
代码:
Sub pressBtn()
'your html declarations & navigation
'Grab the content class that contains your titlename
Dim contentClassColl As Object, contentClass As Object
Set contentClassColl = ie.document.getElementsByClassName("content")
Dim TitleName As String, pColl As Object, p As Object, exitFlag As Boolean
TitleName = "YourTitleNameHere"
'Grab the correct class by looking at each tagname within the class for your title
For Each contentClass In contentClassColl
On Error Resume Next 'in case there are no tag name 'P' in class
Set pColl = contentClass.getElementsByTagName("p")
For Each p In pColl
If p.innerHTML Like "*" & TitleName & "*" Then
exitFlag = True
Exit For
End If
Next p
On Error GoTo 0
If exitFlag = True Then Exit For
Next contentClass
Dim btnColl As IHTMLElementCollection, btn As IHTMLElement
Set btnColl = contentClass.getElementsByTagName("button")
For Each btn In btnColl
If btn.className = "edit" Then
Exit For
End If
Next btn
btn.Click
End Sub