擅长:python、mysql、java
<p>由于返回的格式与元组匹配,因此无需使用返回为“rgba”字符串的“re”即可实现:</p>
<pre><code>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
</code></pre>
<p>如果字符串是“rgb”,只需省略“alpha”</p>
<pre><code>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
</code></pre>
<p>由于提出了最初的问题,现在首选的方法是使用selenium颜色支持模块:</p>
<p>一个简单的指南是<a href="https://selenium-python.readthedocs.org/api.html#module-selenium.webdriver.support.color" rel="nofollow">here</a></p>