python - 如何使用Selenium WebDriver和python获取Web元素的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15102323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a color of a web element using Selenium WebDriver with python?
提问by nids
How do I locate the background-color of a webelement in hexadecimal format? With my current selenium webdriver python code it is returning the background-color in RGB format.
如何以十六进制格式定位 webelement 的背景颜色?使用我当前的 selenium webdriver python 代码,它以 RGB 格式返回背景颜色。
This is the html element that I am looking at
这是我正在查看的 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%"
My webdriver python code is:
我的 webdriver python 代码是:
find_element_by_class_name("bar").get_attribute("style")
It is returning the style with the colors in rgb format. I want to specifically get the background-color in hexadecimal format so that I can compare it with my expected value. I am getting the following output now:
它返回带有 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%;
回答by unutbu
import re
# style = find_element_by_class_name("bar").get_attribute("style")
style = '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%;'
r,g,b = map(int, re.search(
r'background-color: rgb\((\d+),\s*(\d+),\s*(\d+)\)', style).groups())
print('{:X}{:X}{:X}'.format(r, g, b))
yields
产量
DD514C
回答by Carlos V
You're looking for value_of_css_property('background-color'):
您正在寻找value_of_css_property('background-color'):
rgb = find_element_by_class_name("bar").value_of_css_property('background-color')
However, this will return the string rgb(221, 81, 76). In order to get the hex value of it, you can use @unutbu's answer:
但是,这将返回 string rgb(221, 81, 76)。为了获得它的十六进制值,您可以使用@unutbu 的回答:
import re
...
rgb = find_element_by_class_name("bar").value_of_css_property('background-color')
r,g,b = map(int, re.search(
r'rgb\((\d+),\s*(\d+),\s*(\d+)', rgb).groups())
color = '#%02x%02x%02x' % (r, g, b)
And your hex coloris the string #dd514c.
而你的十六进制color是 string #dd514c。
回答by PythonTester
As the format of the return matches a tuple this is achievable without using 're' where the return is an 'rgba' string:
由于返回的格式与元组匹配,因此无需使用“re”即可实现,其中返回是“rgba”字符串:
import ast
rgba = element.value_of_css_property("background-color")
r, g, b, alpha = ast.literal_eval(rgba.strip("rgba"))
hex_value = '#%02x%02x%02x' % (r, g, b)
return hex_value, alpha
Where the string is 'rgb' simply omit the "alpha"
字符串是 'rgb' 的地方只是省略了“alpha”
import ast
rgb = element.value_of_css_property("background-color")
r, g, b = ast.literal_eval(rgb.strip("rgb"))
hex_value = '#%02x%02x%02x' % (r, g, b)
return hex_value
Since the Original question was raised the preferred method is now to use selenium color support module:
由于提出了原始问题,现在首选方法是使用硒颜色支持模块:
A simple guide is here
一个简单的指南在这里
回答by Kundan Kumar
To convert color, you can directly use selenium's Color class:
要转换颜色,可以直接使用 selenium 的 Color 类:
from selenium.webdriver.support.color import Color
rgb = find_element_by_class_name("bar").value_of_css_property('background-color')
hex = Color.from_string(rgb).hex

