selenium/robotframework 中元素的背景颜色

Element's background color in selenium/robotframework

有没有办法获取selenium robotframework中元素的背景颜色?我正在尝试获取属性,但它不起作用 returns None

这里是 html

<li pl-repeat="(slideId, slideContents) in slides" pl-click="viewSlide(slideContents['w-l'].url, slideId)" pl-class="{checked : slideId == viewSlideKey}" class="pl-scope checked"><span class="pl-binding">Draft</span></li>

你可以使用 javascript return document.defaultView.getComputedStyle(document.getElementByID("ID"),null)['background-color']

如果是 class 则相同,但 document.getElementsByClassName("class-name")[0],null)['background-color']

虽然您可以使用 JavaScript 来获取计算样式,但我相信获取样式的惯用方法是使用 Selenium 的 API。一旦你获得了webelement reference using Get Webelement, you can use the value_of_css_property获取背景颜色的方法。

Open Browser    https://www.whosebug.com    gc
${elem}    Get Webelement    css=.post-tag
${bg color}    Call Method    ${elem}    value_of_css_property    background-color

Result: ${bg color} = rgba(225, 236, 244, 1)

我在此测试中注意到的一个区别是 value_of_css_property 返回了 rgba 值,而 getComputedStyle 仅返回了 rgb。

Selenium2LibraryExtension attempts to make this easier by implementing Element Background Color Should Be。 value_of_css_property 也在那里使用。