2013-02-27 13 views
8

Jak znaleźć kolor tła elementu WWW w formacie szesnastkowym? Z moim aktualnym kodem pythonowym weblover selenium zwraca kolor tła w formacie RGB.Jak uzyskać kolor elementu sieci za pomocą Selenium WebDriver z pythonem?

To jest element html, że patrzę na

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

Moja webdriver kodu Pythona:

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

To jest powrót do stylu z kolorami w formacie RGB. Chcę uzyskać konkretny kolor tła w formacie szesnastkowym, aby móc go porównać z moją oczekiwaną wartością. Otrzymuję następujący wynik teraz:

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

Znalazłem wiele rozwiązań za pomocą getCssValue dla Java? Co to jest pythonowy odpowiednik getCssValue? – nids

+0

Odpowiednikiem python jest element.value_of_css_property ("kolor tła"). Ale nie zwraca szesnastkowo (Java nie ma również: https://code.google.com/p/selenium/source/browse/java/client/src/org/openqa/selenium/WebElement.java). Odpowiedź unutbu da ci szesnastkową. – Isaac

Odpowiedz

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

daje

DD514C 
9

Szukacie value_of_css_property('background-color'):

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

Jednak ta zwróci ciąg rgb(221, 81, 76). W celu uzyskania wartości hex z niego, można użyć @ odpowiedź unutbu za:

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) 

i twój hex color jest ciąg #dd514c.

2

jako format powrotu mecze krotki Jest to możliwe bez używania „re”, gdzie zysk jest „rgba” string:

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 

Gdzie ciąg jest „RGB” po prostu pominąć „alfa "

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 

Ponieważ zapytania oryginalny podniesiono preferowaną metodą jest użycie selen obsługa modułu koloru:

prosty przewodnik jest here

Powiązane problemy