Home » excel » excel – get full value from partial id when scraping using VBA and selenium

excel – get full value from partial id when scraping using VBA and selenium

Posted by: admin May 14, 2020 Leave a comment


I am (trying to) scrape information from an intranet page that has a lot of

<div id="CustID_1234567"[...]>
    <div class="logged">2019-09-27</div>
<div id="CustID_2345678"[...]>
    <div class="logged">2019-09-26</div>

I am trying to collect all the customer IDs in an array for further processing but I am having a hard time to find the correct code using selenium for getting the full id.

I have tried different variants of FindElementsByXPath while looping through the code but I don´t know how to get the ID text

    Customers(a)=bot.FindElementsByXPath("//*[contains(@id, 'CustID_')]").Item(a).ID
    Customers(a)=bot.FindElementsByXPath("//*[contains(@id, 'CustID_')]").Item(a).text

The first one (.id) ends in an error but the last (.text) one gets the text (duh), “2019-09-27” and “2019-09-26”, so I guess I am not all that far from the real solution.

Any suggestion, please?

How to&Answers:

Get the “id” attribute from the retrieved element in order to get the full value.

bot.FindElementsByXPath("//*[contains(@id, 'CustID_')]").Item(a).attribute("id")


There is a faster way (faster than xpath) with correct syntax for adding to array, using css selectors. This is also more accurate as it matches on starts with rather than contains.

Dim elem As Object, elems As Object
Set elems = bot.FindElementsByCss("[id^=CustID_]")
For each elem in elems
    Debug.Print elem.Attribute("id")

Storing in an array

Dim elem As WebElement, elems As WebElements, i As Long, arr()
Set elems = bot.FindElementsByCss("[id^=CustID_]")
ReDim arr(1 To elems.Count)

For Each elem In elems
    i = i + 1
    arr(i) = elem.Attribute("id")