0

I am working on selenium in VBA and I have stored a variable"post" to store all the occurrences of a specific element like that

Dim post As Object

Set post = .FindElementsByCss("#DetailSection1")
Dim i As Long
For i = 1 To post.Count
    Debug.Print post.Item(i).getAttribute("style")
Next i

I need to extract the style value from the elements

<div id="DetailSection1" style="z-index:3;clip:rect(0px,746px,32px,0px);top:228px;left:0px;width:746px;height:32px;">
</div>

Also I need to print in the immediate window the innerHTML and when I used getAttribute("innerHTML"), it doesn't work for me Any ideas

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
YasserKhalil
  • 9,138
  • 7
  • 36
  • 95
  • Shouldn't you use getting the element by Id instead of getting it by CSS? – Tomasz Paluch Jun 12 '20 at 08:37
  • The id in my case is not unique. I have 22 of this id – YasserKhalil Jun 12 '20 at 08:44
  • I can't remember but isn't Selenium basic method Attribute("") rather than getAttribute? getAttribute is for a node within nodeList rather than webElement. IIRC but confirmation would be good. – QHarr Jun 12 '20 at 16:02
  • Thank you very much for the awesome hint. That worked for me `Debug.Print .FindElementByCss("#DetailSection1").Attribute("outerHTML")` – YasserKhalil Jun 12 '20 at 21:09

1 Answers1

1

getAttribute("style") should work but you have to induce a waiter for the element to be present/visible within the HTML DOM.

Debug.Print post.Item(i).getAttribute("style")

Precisely, to extract value of the style attributes from the elements you can use the getCssValue() method as follows:

Debug.Print post.Item(i).getCssValue("z-index")
Debug.Print post.Item(i).getCssValue("top")
Debug.Print post.Item(i).getCssValue("left")
Debug.Print post.Item(i).getCssValue("width")
Debug.Print post.Item(i).getCssValue("height")

getAttribute("innerHTML")

get_attribute("innerHTML") can be used to read the innerHTML or the text within any node / WebElement

You can find a detailed discussion in Difference between text and innerHTML using Selenium


References

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352