Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
promo = IE.Document.getElementById("formContent").getElementsByTagName("label")(1)
If InStr(promo.innerText, Range("E5").Value) = 0 Then
ActiveSheet.Range("D" & i) = "No"
Else
ActiveSheet.Range("D" & i) = "Yes"
End If
Next i
IE.Quit
这是HTML,我试图检查E5范围内的值是否与其中一个标签中的值相匹配(根据E5的值而改变)...我可以& #39;似乎让它看透这些标签并告诉我E5是否匹配......难倒。
<div id="formHeader">Personal Information:</div>
<div id="formContent">
Salutation:<br>
<label class="formResult">MR</label> <br>
First Name:<br>
<label class="formResult">John</label> <br/>
Middle Name:<br>
<label class="formResult">P</label> <br/>
Last Name:<br>
<label class="formResult">Smith</label> <br>
Preferred Name:<br>
<label class="formResult">Johnny</label> <br>
Date of Birth: <br>
<label class="formResult">Mar 31, 1985</label> <br/>
Gender:<br>
<label class="formResult">M</label> <br/>
Language:<br>
<label class="formResult">E</label> <br/>
</div>
<br/>
<div id="formHeader">Mailing Address:</div>
<div id="formContent">Address Type: <br/>
<label class="formResult">HOME</label> <br/>
Mail Consent: <br/>
<label class="formResult">Y</label> <br/>
Address 1: <br/>
<label class="formResult">101 Smith St</label> <br/>
Address 2: <br/>
<label class="formResult"></label> <br/>
City: <br/>
<label class="formResult">Happytown</label> <br/>
任何帮助将不胜感激,谢谢!
答案 0 :(得分:0)
迭代所有标签元素的简单循环是:
Dim promos As IHTMLElementCollection
Set promos = ie.Document.getElementById("formContent").getElementsByTagName("label")
For Each promo In promos
If InStr(promo.innerText, Range("E5").Value) = 0 Then
ActiveSheet.Range("D" & i) = "No"
Else
ActiveSheet.Range("D" & i) = "Yes"
End If
Next
但是,如果找到值,则需要退出循环。
逻辑上,将文本设置为&#34;否&#34;并且只将其更改为&#34;是&#34;如果找到了值。然后退出。
ActiveSheet.Range("D" & i) = "No"
Dim promos As IHTMLElementCollection
Set promos = ie.Document.getElementById("formContent").getElementsByTagName("label")
For Each promo In promos
If InStr(promo.innerText, Range("E5").Value) <> 0 Then
ActiveSheet.Range("D" & i) = "Yes"
Exit For
End If
Next