VBA - 在 getElementsByClassName 中使用变量出错

4

当我尝试使用一个变量来指定要使用的类列表中的哪个元素时,我遇到了"对象不支持此属性或方法"(运行时错误'438')错误。

For tdNum = 0 To 1000
    If document.getElementsByClassName("prod-somm")(tdNum).getElementById("no-piece").innerText = ItemNbr Then
            Cells(cell, 2).Value = document.getElementsByClassName("prod-somm")(tdNum).getElementById("col-action").getElementsByTagName("span")(0).innerText
            Exit For
    End If
Next tdNum

HTML:

<table align="center" cellspacing="0" class="prod-somm">
    <tbody>
        <tr>
            <td align="center" rowspan="2" class="prod-somm-image"></td>
            <td class="prod-somm-texte" valign="top">
                <a href="/eng/Balance-of-system/Fuse-and-holder/Wohner-31110.000/p/284" id="no-piece">
                    90-FT017
                    <span class="prod-somm-sepno"></span>
                    <span id="panier_fab_284">Wohner</span>
                    <span id="panier_nomanufact_284">31110.000</span>
                </a>
            <a href="/eng/Balance-of-system/Fuse-and-holder/Wohner-31110.000/p/284"></a>
            </td>
        </tr>
    <tr>
        <td id="col-action">
            <div class="prix">
                <span id="panier_prix_284">10.43</span>
            </div>
        </td>
    </tr>
</table>

问题发生在If document.getElementsByClassName("prod-somm")(tdNum).getElementById("no-piece").innerText = ItemNbr Then
目标是使程序遍历搜索结果列表,直到找到与我的Excel电子表格中匹配的项目编号,然后取出相应的价格。
编辑:这是整个代码。可能会让我想做的事情更清楚一些。
Option Explicit

Function priceGetRematek()

 Dim XMLHttpRequest As New MSXML2.XMLHTTP60
 Dim xhr As MSXML2.XMLHTTP60
 Dim cell As Integer
 Dim tdNum 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 Price Element from HTML
  Set xhr = New MSXML2.XMLHTTP60

  For cell = 2 To 38

      ItemNbr = Cells(cell, 1).Value

      With xhr

          .Open "POST", "https://rematek-energie.com/eng/pg/1/r/" & ItemNbr, False
          .send
'Debug.Print xhr.responseText

          If .readyState = 4 And .Status = 200 Then
                Set document = New MSHTML.HTMLDocument
                document.body.innerHTML = .responseText

                For tdNum = 0 To 1000
                    If document.getElementsByClassName("prod-somm")(tdNum).getElementById("no-piece").innerText = ItemNbr Then
                        Cells(cell, 2).Value = document.getElementsByClassName("prod-somm")(tdNum).getElementById("col-action").getElementsByTagName("span")(0).innerText
                        Exit For
                    End If
                Next tdNum

          Else
                MsgBox "Error" & vbNewLine & "Ready state: " & .readyState & vbNewLine & "HTTP request status: " & .Status
          End If

      End With

  Next cell

End Function

这超出了我的能力范围。没想到 VBA 语法允许两个 () 连续出现。 - findwindow
1
@findwindow 用于直接访问集合或数组的元素。您可以使用 Set y = d.getElementsByClassName("a")(1),而不是 Set x = d.getElementsByClassName("a"): Set y = x(1) - SierraOscar
1
@MacroMan 我只是在测试你 =P - findwindow
1
你是否在使用后期绑定?GetElementsByClassName需要早期绑定。 - SWa
@Kyle,我的专业术语有些不足。早期绑定是指您将变量定义为特定类型,对吗?而不是将其留作对象并稍后设置?如果是这样的话,是的,我正在使用早期绑定。 - ActuallyJane
显示剩余11条评论
1个回答

1
方法getElementById只能在html文档上使用,而不能在html元素上使用。 但是由于您重复使用了id,您最好的选择可能是使用querySelector获取目标元素。 请注意,标准规定唯一id,但浏览器并不强制执行此规定。
以下是一个示例,应该可以帮助您入门:
' execute the query
Dim xhr As New MSXML2.XMLHTTP60
xhr.Open "GET", "https://rematek-energie.com/eng/pg/1/r/5", False
xhr.send

' load the html
Dim html As New HTMLDocument, html2 As Object
Set html2 = html
html2.write xhr.responseText

' scrap the html
Dim elements As MSHTML.IHTMLElementCollection
Dim element As MSHTML.IElementSelector
Dim link As IHTMLElement

Set elements = html.getElementsByClassName("prod-somm")
For i = 0 To elements.Length - 1
  Set element = elements(i)

  ' get the link with id="no-piece"
  Set link = element.querySelector("a[id='no-piece']")
  If Not link Is Nothing Then

    ' display the product code
    Debug.Print link.FirstChild.data

  End If

Next

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接