0

Actually I would like to fetch the CSS class block

HTML Code:

<a id="fld_2418083_6_opt1730754" data-label="F°" data-field="fld_2418083" data-active="btn-primary" data-default="btn-default" class="btn btn-primary" data-value="0" aria-labelledby="fld_2418083Label" title="Choose OptionF°">F°</a>

CSS Code:

.btn-primary{
   background-color: #438bca;
}

I have tried this piece of code for fetching the CSS block in python with selenium but I got an error of

cls_name = element.get_attribute("class")
bg_color = cls_name.value_of_css_property("background-color")
print(f'{ bg_color = }')

Expecting the output is

background-color: #438bcaaliceblue

1 Answer 1

1

You did not include the way you retrieve the element and what's the error you encountered. My guess here is you are using the class name which is very general and could have multiple web elements sharing the same class.

To get that specific element, you can use its ID, like below :

element = driver.find_element(By.ID, 'fld_2418083_6_opt1730754')
bg_color = element.value_of_css_property("background-color")
print(f'{ bg_color = }')

With the output of following :

bg_color = 'rgba(67, 139, 202, 1)'

You can convert it to hex if you like by following :

from selenium.webdriver.support.color import Color

hex = Color.from_string(bg_color).hex
print(f'{ hex = }')

With the output of following:

hex = '#438bca'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.