VBA 从隐藏字段中抓取值

VBA to scrape the value from a hidden field

我是新手 HTML 并且已经尝试了好几天使用 class 名称和 ID 从隐藏字段中抓取值,但我仍然做不到获取值。

我正在尝试从以下 HTML;

中获取值 (4)

<input id="collectionQuantity" type="hidden" value="4">

这是从下面较大的摘录中摘录的;

<div class="lg-24 md-12 cols">
     
 <input id="selectedBranchCode" type="hidden" value="OT4">
 <input id="selectedBranchName" type="hidden" value="Ashton-under-Lyne">
 <input id="collectionQuantity" type="hidden" value="4">
      
  <button id="add_for_collection_button_3730P" title="Click here to add this item to your basket for collection" class="btn btn--lg btn--col fill " data-content="add-to-basket">Click &amp; Collect</button>
  <p id="branch_collection_3730P">4 in stock in <strong>Ashton-under-Lyne</strong> <a href="https://www.screwfix.com/jsp/cpc/cpcCheckStock.jsp?product_id=3730P" id="click_and_collect_3730P" class="_btn--link">Change store</a></p>
             
    </div>

我已经尝试了很多获取价值的方法。 我认为最接近的是;

sh01.Cells(r, 5) = HTML.getElementsByClassName("lg-24 md-12 cols")(3).innertext                                  'product stock
sh01.Cells(r, 5) = HTML.getElementsByTagName("p")(7).innertext                                                   'product stock
sh01.Cells(r, 5) = HTML.getElementById("branch_collection_" & z_sh01.Cells(y, 2)).innertext                      'product stock
sh01.Cells(r, 5) = HTML.getElementsByClassName("lg-24 md-12 cols")(3).getElementById("collectionQuantity").Value 'product stock
sh01.Cells(r, 5) = HTML.querySelector("# branch_collection_" & z_sh01.Cells(y, 2)).innertext                     'product stock
sh01.Cells(r, 5) = HTML.getElementById("collectionQuantity").innertext                                           'product stock

在此先感谢您的帮助。

伊恩

尝试

HTML.querySelector("#collectionQuantity").Value

HTML.getElementById("collectionQuantity").getAttribute("value")

甚至

HTML.getElementById("collectionQuantity").Value

您在目标元素的 value 属性值之后,而不是 .innerText 。以上显示了 3 种方法。


所以,我不得不设置一个本地商店,然后先添加一个不同的导航,以确保在转到感兴趣的页面之前设置了本地商店,否则商店将留空,因此元素的 .value 将留空问题也是空白

Option Explicit

Public Sub GetInfo()
    Dim IE As New InternetExplorer
    With IE
        .Visible = True
        .Navigate2 "https://www.screwfix.com/jsp/tradeCounter/tradeCounterDetailsPage.jsp?id=460"

        While .Busy Or .readyState < 4: DoEvents: Wend

        .document.querySelector("input.btn").Click

        While .Busy Or .readyState < 4: DoEvents: Wend
        .Navigate2 "https://www.screwfix.com/p/hd907-9q-freestanding-oil-filled-radiator-2000w/3730p"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document

            Debug.Print .getElementById("collectionQuantity").Value

        End With

        .Quit
    End With
End Sub