如何获得使用Selenium的webdriver与Python网络元素的颜色? [英] How to get a color of a web element using Selenium WebDriver with python?

查看:1743
本文介绍了如何获得使用Selenium的webdriver与Python网络元素的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何找到十六进制格式的webelement的背景颜色?以我目前的硒的webdriver蟒蛇code它返回RGB格式的背景颜色。

这是我在看的HTML元素

  DIV CLASS =酒吧的风格=背景色:#DD514C;背景图片:-moz-线性梯度(中心顶部,#EE5F5B,#C43C35);背景-image:-webkit-线性渐变(顶部,#EE5F5B,#C43C35); background-image的:-ms-线性渐变(顶部,#EE5F5B,#C43C35);过滤器:进程id:DXImageTransform.Microsoft.gradient(startColorstr =#EE5F5B,endColorstr =#C43C35,GradientType = 0);背景重复:重复 -  X;颜色:#FFFFFF;宽度:11.5%

我的webdriver蟒蛇code是:

  find_element_by_class_name(巴)。get_attribute(风格)

它返回与RGB格式的颜色风格。我想特别获得背景颜色以十六进制格式,这样我可以与我的预期值进行比较。我得到下面的输出现在:

 背景色:RGB(221,81,76);背景图片:-moz-线性梯度(中心顶部,RGB(238,95,91),RGB(196,60,53));背景重复:重复 -  X;颜色:RGB(255,255,255);宽度:11.5%;


解决方案

您正在寻找 value_of_css_property('背景颜色')

  RGB = find_element_by_class_name(巴)。value_of_css_property(背景颜色)

不过,这将返回字符串 RGB(221,81,76)。为了得到它的十六进制值,可以使用@ unutbu的回答是:

 进口重
...
RGB = find_element_by_class_name(巴)。value_of_css_property(背景颜色)R,G,B =地图(INT,re.search(
             r'rgb \\((\\ d +)\\ S *(\\ d +)\\ S *(\\ D +),RGB).groups())
颜色='#%02X%02X%02X%(R,G,B)

和十六进制颜色是字符串#dd514c

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'):

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:

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 color is the string #dd514c.

这篇关于如何获得使用Selenium的webdriver与Python网络元素的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆