首页 > 解决方案 > Selenium VBA中的GetAttribute用于样式

问题描述

我正在使用 VBA 中的 selenium,并且我存储了一个变量“post”来存储所有出现的特定元素,例如

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

我需要从元素中提取样式值

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

另外我需要在即时窗口中打印innerHTML,当我使用它时getAttribute("innerHTML"),它对我不起作用任何想法

标签: excelvbaseleniumselenium-webdrivergetcomputedstyle

解决方案


getAttribute("style")应该可以工作,但您必须诱导一个服务员让元素在HTML DOM中出现/可见。

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

准确地说,要从元素中提取样式属性的值,可以使用getCssValue()如下方法:

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")可用于读取 innerHTML 或任何节点/ WebElement中的文本

您可以在使用 Selenium 的文本和内部 HTML 之间的区别中找到详细讨论


参考

您可以在以下位置找到一些相关的讨论:


推荐阅读