excelvbaseleniumselenium-webdrivergetcomputedstyle

GetAttribute in Selenium VBA for style


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


Solution

  • 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: