excel - getElementById FirstChild 引发运行时错误 91

标签 excel vba

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

我的问题是我不断收到“对象变量或未设置 block 变量”错误,并且没有多少论坛潜水给我一个解决方案。

错误发生在 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
我试图定位的 HTML 是 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 文档 - 没有内容可供选择。

可能这就是你想要的:
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

关于excel - getElementById FirstChild 引发运行时错误 91,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37420044/

相关文章:

excel - 从 SQL Server DB 更新单独工作表中的数据后自动刷新 Excel 2007 数据透视表

VBA - 从右向左分割(反向)

ms-access - 用记录集填充列表框并比较日期

c# - 使用 iText C# 将 HTML 转换为 Excel 文档

VBA:对于 wRange 中的每个 wCell

excel - 如何从 Add in 将 OOXML 数据插入 excel 和 Powerpoint

vba - VBScript 或 VBA 中的 ISO 周数

用于替换部分字符串(包括单引号 ('))的正则表达式

vba - Excel循环,循环固定次数

excel - VBA Excel 根据最后一个逗号拆分字符串