getElementById FirstChild抛出运行时错误91

我正在尝试使用MSXML2类login到网站并将价格下载到我的电子表格。 我有一个代码用于search产品的产品编号列表,然后它应该从html中提取价格元素。

我的问题是,我一直得到“对象variables或块variables没有设置”的错误,没有任何论坛潜水给了我一个解决scheme。

该错误发生在document.getElementById("prix").FirstChild.innerHTML = .responseText

  Option Explicit Function loginRematek() Dim XMLHttpRequest As New MSXML2.XMLHTTP60 Dim xhr As MSXML2.XMLHTTP60 Dim cell As Integer Dim ItemNbr As String Dim document As MSHTML.HTMLDocument 'Login to Rematek With XMLHttpRequest .Open "POST", "https://rematek-energie.com/eng/customer-login/account- authentication.php", False .setRequestHeader "Content-Type", "application/x-www-form-urlencoded" .send "name_se_connecter=se_connecter&zebra_honeypot_se_connecter=&courriel=rob@solacity.com&motpasse=password&connexion=Sign in" End With Debug.Print XMLHttpRequest.responseText 'Get Element Set xhr = New MSXML2.XMLHTTP60 For cell = 1 To 38 ItemNbr = Cells(cell, 1).Value With xhr .Open "GET", "https://rematek-energie.com/eng/pg/1/r/" & ItemNbr, False .send If .readyState = 4 And .Status = 200 Then Set document = New MSHTML.HTMLDocument document.getElementById("prix").FirstChild.innerHTML = .responseText Else MsgBox "Error" & vbNewLine & "Ready state: " & .readyState & _ vbNewLine & "HTTP request status: " & .Status End If Cells(cell, 2).Value = .responseText End With Next cell End Function 

同样,错误发生在document.getElementById("prix").FirstChild.innerHTML = .responseText

我想要的目标是panier_prix_326价值,但ID在每个页面上的变化,因为我的目标是多个页面,我认为这将是更好的目标首先是恒定prix ,然后是该元素的第一个孩子。

 <tr> <td id="col-action"> <div class="prix"> <span id="panier_prix_326">99.40</span> <div id="prix-detail">MSRP: 152.93$</div> </div> </td> </tr> 

 Set document = New MSHTML.HTMLDocument document.getElementById("prix").FirstChild.innerHTML = .responseText 

document是一个空的HTML文档 – 没有内容可供select。

可能这是你想要的:

 If .readyState = 4 And .Status = 200 Then Set document = New MSHTML.HTMLDocument document.body.innerHTML = .responseText Cells(cell, 2).Value = _ document.getElementById("prix").FirstChild.innerHTML Else MsgBox "Error" & vbNewLine & "Ready state: " & .readyState & _ vbNewLine & "HTTP request status: " & .Status End If 

编辑 – 你的HTML没有一个ID“prix”,所以你不能在这里使用getElementById

 <tr> <td id="col-action"> <div class="prix"> <span id="panier_prix_326">99.40</span> <div id="prix-detail">MSRP: 152.93$</div> </div> </td> </tr> 

也许相反:

 Cells(cell, 2).Value = _ document.getElementById("col-action").getElementsByTagName("span")(0).innerText