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.
This is the html element that I am looking at
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:
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:
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%;
You're looking for
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:And your hex
color
is the string#dd514c
.yields
As the format of the return matches a tuple this is achievable without using 're' where the return is an 'rgba' string:
Where the string is 'rgb' simply omit the "alpha"
Since the Original question was raised the preferred method is now to use selenium color support module:
A simple guide is here
To convert color, you can directly use selenium's Color class:
Selenium docs