getElementby ID不返回值VBA

时间:2018-02-10 23:07:20

标签: excel vba web-scraping getelementbyid

我搜索了SO并且没有得到答案,但我认为我很接近。我是编码的新手,所以我很感激帮助。

我使用Hubspot管理客户详细信息并擅长生成报价。我想从Hubspot抓取客户数据并填写报价。

HTML代码

<input type="text" property="PropertyRecord { &quot;calculated&quot;: false, 
;createdUserId&quot;: null, &quot;defaultValue&quot;: undefined, 
;deleted&quot;: null, &quot;description&quot;: &quot;A contact's first 
name&quot;, &quot;displayMode&quot;: &quot;current_value&quot;, 
;displayOrder&quot;: 0, &quot;externalOptions&quot;: false, 
;favorited&quot;: true, &quot;favoritedOrder&quot;: 0, 
;fieldType&quot;: &quot;text&quot;, &quot;filterName&quot;: 
&quot;&quot;, &quot;formField&quot;: true, &quot;groupName&quot;: 
;contactinformation&quot;, &quot;hidden&quot;: false, 
;hubspotDefined&quot;: true, &quot;label&quot;: &quot;First Name&quot;, 
;mutableDefinitionNotDeletable&quot;: true, &quot;name&quot;: 
;firstname&quot;, &quot;numberDisplayHint&quot;: null, 
;objectType&quot;: undefined, &quot;options&quot;: List [], 
;optionsAreMutable&quot;: null, &quot;placeholder&quot;: &quot;&quot;, 
;prospectType&quot;: null, &quot;readOnlyDefinition&quot;: true, 
;readOnlyValue&quot;: false, &quot;referencedObjectType&quot;: null, 
;searchable&quot;: true, &quot;showCurrencySymbol&quot;: null, 
;sortable&quot;: true, &quot;textDisplayHint&quot;: null, 
;type&quot;: &quot;string&quot;, &quot;updatedUserId&quot;: 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

什么是更好的解决方案?

由于

1 个答案:

答案 0 :(得分:1)

尝试以下方法。您不能像上面那样在for循环中使用.getElementById()。 ID包含单个元素。但是,我使用.querySelector()来执行此操作,因为ID不是静态的,您也无法在for循环中使用它。

Set firstname = ieDoc.querySelector("[id^='uid-ctrl-']")
MsgBox firstname.getAttribute("value")