大家好,欢迎来到我的博客!今天我要和大家分享一个关于使用VBA代码定位网页元素的技巧,让你的网页自动化处理更加得心应手。不管你是想自动填写表单,还是抓取特定信息,这些技巧都会让你事半功倍。
背景知识
在进行VBA自动化操作时,我们经常会使用Internet Explorer(IE)来模拟浏览器行为。这篇文章将着重介绍如何使用VBA代码定位网页上的元素,以便我们可以与之交互。
对于初学者来说,在了解了DOM的结构之后首先要学会的就是如何找到元素,下面代码是定位各类元素的方法示例。
定位元素 获取元素options所在的document Set oDom = options.document 获取元素options所在的IE对象 Set oIE = options.document.parentWindow 获取document内容:document.body.innerhtml Set repList = doc.getElementsByClassName('reportList') 'only works in IE9, not in IE8 Set repList = doc.querySelectorAll('.reportList') 'this works in IE8+ 对于<input name="username" id="username_id" class="gray">获得tag的几种方法: WebBrowser1.Document.getElementsByName("username") WebBrowser1.Document.getElementById("username_id") WebBrowser1.Document.getElementsByTagName("input")(0) WebBrowser1.Document.getElementsByTagName("input")("username") WebBrowser1.Document.getElementsByTagName("input")("username_id") WebBrowser1.Document.all(0) WebBrowser1.Document.all("username") WebBrowser1.Document.all("username_id") WebBrowser1.Document.all.item("username") WebBrowser1.Document.all.item("username_id") WebBrowser1.Document.all.tags("input")(0) Tag 获得某一类tag的集合的方法: WebBrowser1.Document.all.tags("input") 另外部分tag有自己独有的方法,例如: WebBrowser1.Document.links WebBrowser1.Document.frames WebBrowser1.Document.forms WebBrowser1.Document.images WebBrowser1.Document.scripts WebBrowser1.Document.all.tags("select").options 表格 行是table.rows,列则是table.rows(0).cells .document.getElementsByclassname("x-grid3-row-table ")(3).Rows(0).Cells(4).innertext 获取网页(表格)超链接 For i = 0 To TableX.Rows.Length - 1 Set Row = TableX.Rows(i) '遍历每行所有表格 For j = 0 To Row.Cells.Length - 1 Rng.Cells(i + 1, j + 1) = Row.Cells(j).innerText Next Next 对象 DOM(Document Object Model)一般包含以下几种对象: Document:文档对象 Element:元素对象 Attribute:属性对象 Text:文本对象 Comment:注释对象 Node:节点对象,其他5个的父对象 挺好的例子,DOM方式创建一个XML文件 https://www.cnblogs.com/zzstone/p/5562543.html 一般说来,我们想要的网页内容,就在WebBrowser1.document.body.innerhtml和WebBrowser1.document.body.innertext中,通过替换、分析,得到我们想要的数据。 Innerhtml中的内容就是网页源码。 innertext中的内容则是网页上所显示的内容。 下拉菜单 获取选中项的内容 '找到下拉列表对象 Set selectEle = doc.getElementById("UCBody1_ddlproblemgrade") '获取选中的选项内容 Dim selectedText As String selectedText = selectEle.item(selectEle.selectedIndex).innerText 我自己的下拉菜单程序,设置选中某一项 Private Sub selectOption(options As Object, title As String) For Each opt In options If opt.title = title Then opt.Selected = True Else opt.Selected = False End If Next If Not IsNull(options.getAttribute("onchange")) Then options.onchange '只有具有onchange属性的下拉菜单再运行onchange Application.Wait Now + TimeSerial(0, 0, 1) End Sub 获得下拉菜单里的值: .Document.getElementById("selectProductClassify").All.tags("option")(0).Text 选择下拉菜单 .document.All.tags("OPTION")(54).Selected = True 单选按钮radio .getElementById("UCHead_rbtnMaterial_1").Checked = True '是否来自预选库,选否 .getElementById("UCHead_rbtnMaterial_1").Click 框架结构iframe set iframe=.document.getelementbyid("ueditor_0") iframe.contentwindow.document.getelementbyid("SELECT1618565954619").options(2).selected=true 框架网页单击链接: .document.frames(0).document.getelementbyid("A5").Click 属性 设置属性 .getElementById("UCHead$txtRemark").setAttribute "style", "background-color:red;color:white;" 调用js实现 .document.parentWindow.execScript ("document.getElementById('UCHead_txtRemark').setAttribute('style','background-color:red;color:white;');") 获取属性值 获得tag的Value值,和上面类似也有两种方法: vtag.src vtag.getAttribute("src") HTMLElement.getAttribute("data-elm-id") = "field_property_type" 父子节点 增加和移除节点 Set nd = Document.appendChild(el) Document.removeChild nd 前者返回下一个同胞,后者返回前一个同胞 nextSibling/previousSibling Set nexNode = oDom.getElementById("item1").NextSibling Set preNode = oDom.getElementById("item2").PreviousSibling 获取父节点 Set pdlTbl = pdlTbl.getelementbyid(pdlID) Set parentParent = pdlTbl.parentElement Set parNode = oDom.getElementsByTagName("li")(0).ParentNode MsgBox parNode.nodeName MsgBox parNode.innerText MsgBox parNode.innerHTML 获取子节点 Set parentParent = pdlTbl.childnodes.item(0) .getElementById("标签的父对象").all.tags("标签") firstChild 获取节点中的首个子节点,如果不具有子节点则返回null lastChild 获取节点中的末个子节点,如果不具有子节点则返回null nextSibling 获取节点所在的节点列表中下一个节点 nodeName 获取节点的名称 nodeType 获取节点的类型,(参见下表) nodeValue 获取文本节点值 parentNode 获取节点的父节点,如果不具有父节点则返回null 其他节点参考: https://blog.csdn.net/sinat_25419171/article/details/43405317?utm_source=app&app_version=5.5.0 图片 对于部分tag可以通过自己的专有集合方式访问,例如<img name="img1">: WebBrowser1.Document.images(0) WebBrowser1.Document.images("img1") 当然也可以使用如下方法: WebBrowser1.Document.tags("img")(0) 焦点放在某个元素 objIE.document.all.search_keywords.focus() 超链接 一般使用getElementsByTagName获取超链接元素<a>的集合对象,如下: mytable.all.tags("td")(n - 7).getelementsbytagname("a")(0).href 滚动加载(亲测可用) Dim myDiv As Object Dim elemRect As Object Set myDiv = .document.getElementById("item4") Set elemRect = CallByName(myDiv, "getBoundingClientRect", VbMethod) Do Until elemRect.bottom > 0 currPage.parentWindow.scrollBy 0, 0 Set elemRect = CallByName(myDiv, "getBoundingClientRect", VbMethod) Loop myDiv.ScrollIntoView 当元素不在视图中时,它为零,一旦它在视图中,它便成为非零数字。有关更多信息,请参见here 最后,使用myDiv.ScrollIntoView将浏览器移至最底端。
小结
通过这些简单而又强大的VBA代码,你可以在IE中定位并与网页元素进行交互。无论是填写表单、点击按钮还是抓取信息,这些技巧都会让你事半功倍。希望本文能够帮助你在VBA自动化领域迈出坚实的一步。
如果你想了解更多有关VBA、自动化以及编程的知识,记得常来我的博客哦!感谢阅读,我们下次再见!
发表回复