I am writing an automation test script using Robot Framework & Selenium2Library for testing our web application. One of my test cases involves checking the CSS style attribute of an HTML tag.

Is there any option in Robot Framework to obtain the CSS style attributes of an HTML element?

Here is my HTML:

<div id="check_style" style="width:20px;height:20px;background-color:#ffcc00;"></div>

Now, I have to store the background color of this particular html tag into a variable ${bg_color}. Is there any specific keyword in Robot Framework to do this process?

Can you please suggest an effective way to handle this situation?

  • Similar question was here. The javascript bit might be hacky but it does the job.
    – Mache
    Apr 2, 2018 at 20:20

3 Answers 3


You can use the Selenium2Library Get Element Attribute keyword to get the style attribute:

| | ${style}= | Get element attribute | id=check_style@style

You can then either use a regular expression to find the background color attribute or do some additional parsing. The latter would be easier to do in python than with robot keywords.

For example, if you understand regular expressions, something like the following might work. Of course, you'll probably want to add some bullet-proofing.

| | ${style}= | get element attribute | id=check_style@style
| | ${color}= | evaluate | re.search("background-color: *(.*?);", '''${style}''').group(1) | 

Note: you might not get the same literal value as is in the raw HTML. For example, on my machine ${color} comes back as rgb(255, 204, 0) even though the color in the HTML is #ffcc00.


You will find the answer here.
For reference, in this case I would use the keyword Get Element Attribute and compare it against the expected result.

  • I have also tried Get Element Attribute ! Here is my code : ${bg_color} = Get Element Attribute id=check_style . But while executing this code, am getting an error Attribute locator id=check_style doesn't contain an element locator ! Is it the right way to use this specific keyword ? Can you please provide an example ? How to obtain a particular attribute ( eg. background-color ) using this keyword ? Oct 27, 2015 at 11:04
  • @ArunRamachandran You may be experiencing a spacing issue, it's hard to know from a comment what the issue is, you should add the things you tried to your question.
    – Cronax
    Oct 27, 2015 at 11:07
  • Can you please provide an example by using `Get Element Attribute' ! I have searched alot , But couldn't find any proper example related to this specific keyword. Oct 27, 2015 at 11:15

You first need to get the attribute of the element by using 'Get Element Attribute' of selenium2Library:

  ${style}= | Get element attribute | id=check_style@style

Once you get the attribute like "width:20px;height:20px;background-color:#ffcc00;"

then, you can get background-color very easily from this string by using 'Split String' of String library:

 ${style} | ${bgcolor} | Split String | ${style} | background-color:

You will get the back ground color in ${bgcolor}:

Log | ${bgcolor}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.