Home > OS >  VBA Selenium - return all elements with specific attribute
VBA Selenium - return all elements with specific attribute

Time:02-25

I'm using Selenium in Excel VBA to scrape some websites. I'm hoping to find all elements on a webpage which look similar to the snippet below:

<div data-automation-id="racecard-outcome-name"><span >1. The Running Man</span><span >&nbsp;(5)</span></div>

In particular, I'm hoping to find all elements which contain the attribute data-automation-id and then filter by elements which have racecard-outcome-name as this attribute, but this should be easy once I can return all elements with this attribute.

Currently I'm using something similar to

table = bot.FindElementsBy.Attribute("data-automation-id") but I know this is the incorrect use of this operator. Please let me know if you can point me in thr right direction.

CodePudding user response:

Thank you to QHarr for providing a helpful comment. I ended up implementing the following solution to find elements where the data-automation-id had been tagged racecard-outcome-name:

    Set table = bot.FindElementsByCss("[data-automation-id]")
    
    Dim i As Integer, tableCount As Integer
    tableCount = table.count
    For i = 1 To table.count
        If table(i).Attribute("data-automation-id") = "racecard-outcome-name" Then
            MsgBox table(i).Text
        End If
    Next i
  • Related