情况:
我目前正在尝试使用here中的CSS选择器练习在VBA中重现具有语法[attr]
的属性选择器。
选择器用于根据给定属性的值选择元素。
预期结果:
在我添加的html示例中,使用id
尝试获取所有 html.querySelectorAll("[id]")
属性的预期结果在运行时以黄色突出显示。
问题:
我没有获得与id
元素相关的信息 - 黄色突出显示的位 - 我正在获得更多文本。看起来几乎所有东西都有一些重复的材料。
我尝试了什么:
id
。这非常有效。例如:
Set a = html.querySelectorAll("[id=""my-Address""]")
在我的代码示例中,这产生了预期值:
<p id="my-Address">I live in Duckburg</p>
[]
移除[id]
,Option Explicit
'[attribute] [target] Selects all elements with a target attribute e.g. [id]
Public Sub Test13()
Dim html As MSHTML.HTMLDocument, i As Long
Set html = GetTestHTML()
Dim a As Object
'Set a = html.querySelectorAll("[id=""my-Address""]")
Set a = html.querySelectorAll("[id]")
For i = 0 To a.Length - 1
Debug.Print a(i).innerText
Next i
End Sub
Public Function GetTestHTML(Optional ByVal url As String = "https://www.w3schools.com/cssref/trysel.asp") As HTMLDocument
Dim http As New XMLHTTP60
Dim html As New HTMLDocument
With http 'Set http = CreateObject("MSXML2.XMLHttp60")
.Open "GET", url, False
.send
html.body.innerHTML = .responseText
Set GetTestHTML = html
End With
End Function
没有向立即窗口打印任何内容。问题:
如何在VBA中正确编写CSS选择器,以从给定的webpage中提取具有id属性的所有元素?
代码:
<div class="noSel">
<h1><span class="markup"><h1></span>Welcome to My Homepage<span class="markup"></h1></span></h1>
<div id="helpIntro">
<span class="markup"><div class="intro"></span>
<div class="intro">
<p style="margin-top:4px;"><span class="markup"><p></span>My name is Donald <span id="Lastname" style="border-color: rgb(255, 102, 102); background-color: rgb(255, 255, 153);"><span class="markup"><span id="Lastname"></span>Duck.<span class="markup"></span></span></span><span class="markup"></p></span></p>
<p id="my-Address" style="border-color: rgb(255, 102, 102); background-color: rgb(255, 255, 153);"><span class="markup"><p id="my-Address"></span>I live in Duckburg<span class="markup"></p></span></p>
<p style="margin-bottom:4px;"><span class="markup"><p></span>I have many friends:<span class="markup"></p></span></p>
</div>
<span class="markup"></div></span>
</div>
<br>
<div class="helpUl" style="border-color: rgb(255, 102, 102); background-color: rgb(255, 255, 153);">
<span class="markup"><ul id="Listfriends></span>
<ul id="Listfriends" style="margin-top:0px;margin-bottom:0px;">
<li><span class="markup"><li></span>Goofy<span class="markup"></li></span></li>
<li><span class="markup"><li></span>Mickey<span class="markup"></li></span></li>
<li><span class="markup"><li></span>Daisy<span class="markup"></li></span></li>
<li><span class="markup"><li></span>Pluto<span class="markup"></li></span></li>
</ul>
<span class="markup"></ul></span>
</div>
<ul style="display:none;"></ul>
<p><span class="markup"><p></span>All my friends are great!<span class="markup"><br></span><br>But I really like Daisy!!<span class="markup"></p></span></p>
<p lang="it" title="Hello beautiful"><span class="markup"><p lang="it" title="Hello beautiful"></span>Ciao bella<span class="markup"></p></span></p>
<h3><span class="markup"><h3></span>We are all animals!<span class="markup"></h3></span></h3>
<p><span class="markup"><p></span><span><b><span class="markup"><b></span>My latest discoveries have led me to believe that we are all animals:<span class="markup"></b></span></b></span><span class="markup"></p></span></p>
<div class="helpTable" style="width:220px;">
<span class="markup"><table></span>
<ul style="display:none;"></ul>
<div class="noSel" style="margin-top:10px;">
HMTL预期结果为黄色:
="**Data Reporting "&Format(today,"dd, MMM yyyy")&"**"
&#13;
参考文献:
项目参考:
*通过VBE&gt;工具&gt;参考
答案 0 :(得分:1)
原来,有两个错误需要纠正。
"
部分中的结尾<ul id="Listfriends>
。这应该是<ul id="Listfriends">
。这意味着CSS选择器会进行匹配。iframe
中的HTML以便仅使用预期的ID。代码:
Option Explicit
Public Sub GetInfo()
Dim html As MSHTML.HTMLDocument, i As Long
Set html = GetTestHTML()
Dim a As Object
html.body.innerHTML = html.querySelector("#iframeResult").document.getElementById("selectorResult").innerHTML
Set a = html.querySelectorAll("[id]")
For i = 0 To a.Length - 1
Debug.Print a(i).innerText
Next i
End Sub
Public Function GetTestHTML(Optional ByVal url As String = "https://www.w3schools.com/cssref/trysel.asp") As HTMLDocument
Dim http As New XMLHTTP60
Dim html As New HTMLDocument
With http 'Set http = CreateObject("MSXML2.XMLHttp60")
.Open "GET", url, False
.send
html.body.innerHTML = Replace(.responseText, """Listfriends", """Listfriends""")
Set GetTestHTML = html
End With
End Function