vba - 表单提交后如何从 IE 获取对象的更新值?
问题描述
我正在使用 VBA 和 IE 自动提交表单。我可以提交表单,但是,在表单提交后,我无法获得 IE 文档中标签的最新状态 这是我用于 URL http://www.tangoloans.co 的代码。英国/brokerform/?affl_id=216
URL = " http://www.tangoloans.co.uk/brokerform/?affl_id=216 " recordCount = Sheets("Data").Cells(Rows.Count, "A").End(xlUp).row
Set appIE = New InternetExplorer
appIE.navigate URL
Do While appIE.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Waiting ..."
DoEvents
Loop
appIE.Visible = True
Set Doc = appIE.document
Doc.getElementById("title").Value = .Range("A" & row)
Doc.getElementById("fname").Value = .Range("B" & row)
Doc.getElementById("lname").Value = .Range("C" & row)
Doc.getElementById("dobDay").Value = .Range("D" & row)
Doc.getElementById("dobMonth").Value = .Range("E" & row)
Doc.getElementById("dobYear").Value = .Range("F" & row)
Doc.getElementById("l_amount").Value = .Range("M" & row)
Doc.getElementById("l_terms").Value = .Range("N" & row)
Doc.getElementById("h_phone").Value = .Range("G" & row)
Doc.getElementById("m_phone").Value = .Range("H" & row)
Doc.getElementById("address1").Value = .Range("J" & row)
Doc.getElementById("town_city").Value = .Range("K" & row)
Doc.getElementById("p_code").Value = .Range("L" & row)
Doc.getElementById("email").Value = .Range("I" & row)
If .Range("O" & row) = "Tenant" Then
Doc.all("owner")(1).Click
Else
Doc.all("owner")(0).Click
End If
If .Range("P" & row) = "Tenant" Then
Doc.all("guarantor")(1).Click
Else
Doc.all("guarantor")(0).Click
End If
Doc.getElementById("contactViaPost").Checked = True
Doc.getElementById("contactViaEmail").Checked = True
Doc.getElementById("contactViaPhone").Checked = True
Doc.getElementById("agreeToTOS").Checked = True
Doc.getElementById("submit_application").Click
Do While appIE.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Waiting ..."
Application.Wait DateAdd("s", 1, Now)
Loop
Application.Wait DateAdd("s", 20, Now)
Set HCollection = appIE.document.getElementsByTagName("h2")
For Each c In HCollection
If c.className = "vc_custom_heading" Then
.Range("Q" & row) = c.innerText
Exit For
End If
Next
然而这条线
.Range("Q" & row) = c.innerText
总是把旧文本还给我
我尝试增加等待时间,但不是运气。有人可以在这里帮忙吗
解决方案
您是循环c
中的值,它是一个对象,其中包含来自object的标签名称(使用 检索)。For..Each
HCollection
getElementsByTagName
appIE.Document
appIE.Document
是包含在 中的 HTML 的快照appIE
,当前是您最后一次“复制”HTML 时使用的行:
Set Doc = appIE.Document
再次运行该行,对象.Document
属性的“快照”值appIE
将被“刷新”以包含当前在 InternetExplorer
对象中的 HTML。
更多信息:
- MSDN :
Document
属性(Windows/IE) - MSDN :使用浏览器的不同方式(Windows/IE)
- Mozilla:文档属性