Search code examples
htmlvbaseleniumwebautomation

Web Automation using Excel VBA with SeleniumBasic. I cannot access the values I have found


I am trying to load the messages that I find by WDrv.FindElementsByXPath("//*[@id='divMessageLines']/div") but I do not know how to access the individual values that I find. There are an unknown number of values for each search.

The HTML segment is: enter image description here

In this example, if I do a .count I have 2 matching elements:

Dim cntr As Integer
cntr = WDrv.FindElementsByXPath("//*[@id='divMessageLines']/div").Count
‘cntr = 2

I can access each of the two values by using the div1 and div[2]. This is great but there will not always be two values:

cntr = WDrv.FindElementsByXPath("//*[@id='divMessageLines']/div[1]").Count
‘cntr = 1

If I could use a variable, I could loop through all the elements. But it doesn’t seem to like that. It would be great if I could replace the div1 and div[2] with, for example, div[i] but, when I try this, the cntr always equals 0 so I assume it isn't working:

Cntr = WDrv.FindElementsByXPath("//*[@id='divMessageLines']/div[i]").Count
‘cntr = 0

I have this but it just does nothing and stops running the program (no error message):

Dim div As TableElement
Set div = WDrv.FindElementsByXPath("//*[@id='divMessageLines']/div")

So, I am not sure how to access the individual elements to, is there a way?


Solution

  • What about something like

    Dim itm
    
    For Each itm in WDrv.FindElementsByXPath("//*[@id='divMessageLines']/div")
        'do something with itm
        Debug.Print itm.Text ' for example
    Next itm