2013-02-27 11 views
8

Come si individua il colore di sfondo di un elemento Web in formato esadecimale? Con il mio attuale codice Python per il selenio web, restituisce il colore di sfondo in formato RGB.Come ottenere un colore di un elemento Web utilizzando Selenium WebDriver con python?

Questo è l'elemento HTML che sto guardando

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%" 

mio WebDriver codice Python è:

find_element_by_class_name("bar").get_attribute("style") 

sta tornando lo stile con i colori in formato RGB. Voglio ottenere in particolare il colore di sfondo in formato esadecimale in modo da poterlo confrontare con il mio valore atteso. Sto ottenendo il seguente output ora:

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%; 
+0

Ho trovato molte soluzioni utilizzando getCssValue per Java? Qual è l'equivalente Python di getCssValue? – nids

+0

L'equivalente python è element.value_of_css_property ('background-color'). Ma non restituisce esadecimale (nemmeno Java: https://code.google.com/p/selenium/source/browse/java/client/src/org/openqa/selenium/WebElement.java). la risposta di unutbu ti darà esadecimale. – Isaac

risposta

0
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)) 

cede

DD514C 
9

Siete alla ricerca di value_of_css_property('background-color'):

rgb = find_element_by_class_name("bar").value_of_css_property('background-color') 

Tuttavia, questo restituirà la stringa rgb(221, 81, 76). Al fine di ottenere il valore esadecimale di esso, è possibile utilizzare @ risposta di 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) 

E il tuo esadecimale color è la stringa #dd514c.

2

Poiché il formato del ritorno corrisponde una tupla questo è realizzabile senza l'utilizzo di 're' il cui rendimento è un 'RGBA' stringa:

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 

Dove la stringa è 'rgb' semplicemente omettere il "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 

Poiché la domanda originale è stata sollevata il metodo preferito è ora di utilizzare selenio modulo di supporto colore:

Una guida semplice è here

Problemi correlati