IE使用VBA进行屏幕抓取

时间:2017-09-19 10:38:53

标签: excel vba excel-vba web-scraping

我试图点击"下一步"链接在网页中但link.innerHTML无法找到"下一步"

HTML代码如下所示

a href =" queue_monitor.do?task_name = WAITCUST& pager_offset = 250& actionType = next">
下一页
                                                / A>

当我从网页编辑链接以删除下一行及其工作空间时...

For Each ie In objshell.Windows
    If TypeName(ie.Document) = "HTMLDocument" Then
        If InStr(ie.Document.Title, "Monitor") Then
            ie.Visible = False
            ie.Visible = True

            Set html = ie.Document
            Set ElementCol = html.getElementsByTagName("a")

                For Each link In ElementCol

                    If TRIM(link.innerHTML) = "Next" Then

                        link.Click

                            Application.Wait Now + TimeValue("00:00:04")
                            Application.SendKeys ("^a")
                            Application.Wait Now + TimeValue("00:00:04")
                            Application.SendKeys ("^c")
                            Application.Wait Now + TimeValue("00:00:04")
                            ThisWorkbook.Sheets("WAITCUST Raw").Range("A10000").End(xlUp).Offset(3, 0).PasteSpecial
                            Application.Wait Now + TimeValue("00:00:07")
                    End If

                Next link

        End If
    End If
Next ie

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您可以使用CSS选择器通过href值的结尾部分为目标元素进行定位:

[href$='actionType=next']

这是一个属性选择器,用于查找其值以href结尾($)的actionType=next

ie.document.querySelector("actionType=next").Click

HTML查询CSS:

enter image description here