getElementbyID 没有返回值 VBA
getElementby ID not returning value VBA
我搜索过 SO 但未能得到答案,但我想我已经接近了。我是编码新手,所以非常感谢您的帮助。
我使用 Hubspot 管理客户详细信息并 excel 生成报价。我想从 Hubspot 抓取客户数据并用它填充报价。
HTML代码
<input type="text" property="PropertyRecord { "calculated": false,
;createdUserId": null, "defaultValue": undefined,
;deleted": null, "description": "A contact's first
name", "displayMode": "current_value",
;displayOrder": 0, "externalOptions": false,
;favorited": true, "favoritedOrder": 0,
;fieldType": "text", "filterName":
"", "formField": true, "groupName":
;contactinformation", "hidden": false,
;hubspotDefined": true, "label": "First Name",
;mutableDefinitionNotDeletable": true, "name":
;firstname", "numberDisplayHint": null,
;objectType": undefined, "options": List [],
;optionsAreMutable": null, "placeholder": "",
;prospectType": null, "readOnlyDefinition": true,
;readOnlyValue": false, "referencedObjectType": null,
;searchable": true, "showCurrencySymbol": null,
;sortable": true, "textDisplayHint": null,
;type": "string", "updatedUserId": null }"
value="Jonnevie" id="uid-ctrl-54" data-field="firstname" data-selenium-
test="property-input-firstname" autocomplete="off" class="form-control
private-form__control private-form__control--inline isInline">
我正在使用以下代码抓取并获得 Run Rime Error 424
Dim ie As InternetExplorer
Dim ieDoc As HTMLDocument
Dim firstname As Object
Dim url As Variant
Dim ID As Variant
Dim Content As String
Dim i As Integer
Set ieApp = New InternetExplorer
ieApp.Visible = 1
url = "https://app.hubspot.com/sales/3425759/contact/379701/?interaction=note"
ieApp.navigate url
Do While ieApp.Busy:
DoEvents: Loop
Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
Dim ele As Object
Set ieDoc = ieApp.document
For Each ele In ieDoc.getElementById("uid-ctrl-8")
Debug.Print (ele.text)
Next ele
ieApp.Quit
什么是更好的解决方案?
谢谢
试试下面的方法。您不能像上面那样在 for 循环中使用 .getElementById()
。 ID 包含单个元素。但是,我使用 .querySelector()
来解决这个问题,因为 ID 不是静态的,您也不能在 for 循环中使用它。
Set firstname = ieDoc.querySelector("[id^='uid-ctrl-']")
MsgBox firstname.getAttribute("value")
我搜索过 SO 但未能得到答案,但我想我已经接近了。我是编码新手,所以非常感谢您的帮助。
我使用 Hubspot 管理客户详细信息并 excel 生成报价。我想从 Hubspot 抓取客户数据并用它填充报价。
HTML代码
<input type="text" property="PropertyRecord { "calculated": false,
;createdUserId": null, "defaultValue": undefined,
;deleted": null, "description": "A contact's first
name", "displayMode": "current_value",
;displayOrder": 0, "externalOptions": false,
;favorited": true, "favoritedOrder": 0,
;fieldType": "text", "filterName":
"", "formField": true, "groupName":
;contactinformation", "hidden": false,
;hubspotDefined": true, "label": "First Name",
;mutableDefinitionNotDeletable": true, "name":
;firstname", "numberDisplayHint": null,
;objectType": undefined, "options": List [],
;optionsAreMutable": null, "placeholder": "",
;prospectType": null, "readOnlyDefinition": true,
;readOnlyValue": false, "referencedObjectType": null,
;searchable": true, "showCurrencySymbol": null,
;sortable": true, "textDisplayHint": null,
;type": "string", "updatedUserId": null }"
value="Jonnevie" id="uid-ctrl-54" data-field="firstname" data-selenium-
test="property-input-firstname" autocomplete="off" class="form-control
private-form__control private-form__control--inline isInline">
我正在使用以下代码抓取并获得 Run Rime Error 424
Dim ie As InternetExplorer
Dim ieDoc As HTMLDocument
Dim firstname As Object
Dim url As Variant
Dim ID As Variant
Dim Content As String
Dim i As Integer
Set ieApp = New InternetExplorer
ieApp.Visible = 1
url = "https://app.hubspot.com/sales/3425759/contact/379701/?interaction=note"
ieApp.navigate url
Do While ieApp.Busy:
DoEvents: Loop
Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
Dim ele As Object
Set ieDoc = ieApp.document
For Each ele In ieDoc.getElementById("uid-ctrl-8")
Debug.Print (ele.text)
Next ele
ieApp.Quit
什么是更好的解决方案?
谢谢
试试下面的方法。您不能像上面那样在 for 循环中使用 .getElementById()
。 ID 包含单个元素。但是,我使用 .querySelector()
来解决这个问题,因为 ID 不是静态的,您也不能在 for 循环中使用它。
Set firstname = ieDoc.querySelector("[id^='uid-ctrl-']")
MsgBox firstname.getAttribute("value")