Search code examples
vbaloopsclassselenium-webdriverwebdriver

Loop to get the name of every class using Selenium VBA


I'm trying to go through every "class" defined, and get his content (text).

I have code that takes the text from one class, and it doesn't continue with the other one.

It repeats the action 7 times, but just with the first class and doesn't show any error.

Dim bot As New WebDriver, myproducts As WebElements, myproduct As WebElement, i As Integer, productnum As String, clicknum As Integer, mysheet As Worksheet

i = 2
Set mysheet = Sheets("example2")
Set myproducts = bot.FindElementsByXPath("//h1[@class='minificha__nombre-producto']")
For Each myproduct In myproducts
    If myproduct.FindElementByXPath("//span[@class='minificha__sku ng-binding']").Text <> "" Then
        mysheet.Cells(i, 1).Value = myproduct.FindElementByXPath("//span[@class='minificha__sku ng-binding']").Text
        i = i + 1
    End If
Next
MsgBox ("complete")
End Sub

Solution

  • I think you want following sibling. The element you are after is not a child of myproduct. It is the adjacent sibling (same level next element).

    myproduct.FindElementByXPath("following-sibling::span").Text