【发布时间】:2013-02-12 16:24:15
【问题描述】:
如何定位十六进制格式的网页元素的背景颜色?使用我当前的 selenium webdriver python 代码,它以 RGB 格式返回背景颜色。
这是我正在查看的 html 元素
div class="bar" style="background-color: #DD514C; background-image: -moz-linear-gradient(center top , #EE5F5B, #C43C35); background-image: -webkit-linear-gradient(top , #EE5F5B, #C43C35); background-image: -ms-linear-gradient(top , #EE5F5B, #C43C35); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#EE5F5B, endColorstr=#C43C35, GradientType=0); background-repeat: repeat-x; color: #ffffff; width: 11.5%"
我的 webdriver python 代码是:
find_element_by_class_name("bar").get_attribute("style")
它正在返回带有 rgb 格式颜色的样式。我想专门获取十六进制格式的背景颜色,以便我可以将它与我的预期值进行比较。我现在得到以下输出:
background-color: rgb(221, 81, 76); background-image: -moz-linear-gradient(center top , rgb(238, 95, 91), rgb(196, 60, 53)); background-repeat: repeat-x; color: rgb(255, 255, 255); width: 11.5%;
【问题讨论】:
-
我找到了很多使用getCssValue for Java的解决方案? getCssValue 的 python 等价物是什么?
-
python 等效项是 element.value_of_css_property('background-color')。但它不返回十六进制(Java 也不返回:code.google.com/p/selenium/source/browse/java/client/src/org/…)。 unutbu 的答案会给你十六进制。
-
谁能帮助我告诉我如何使用python更改这个{RGB}值
标签: python selenium automation selenium-webdriver