Search code examples
hexrobotframeworkrgb

How to get background-color in hex format from element using Robot Framework


I try this way and use SeleniumLibrary and I use Python 2.7.16

    ${elements}    Get Webelement    (//div[@class="slds-col call-scripticon"])[1]
    ${bg color}    Call Method    ${elements}    value_of_css_property    background-color

but I got rgb(36, 6, 66) I don't know how to get color in #240642 format

HTML

<div class="slds-col call-scripticon" data-aura-rendered-by="2:7200;a" xpath="1"></div>

Style

    element.style {
}
.call-scripticon {
   background-color: #240642;
}

Anyone help me please.


Solution

  • You could create a keyword in python that transform colors from RGB to HEX.

    Let's say you create a module named testLibrary.py in where you will create the python method that change the format of colors:

    def transform_RGB_to_HEX(arg1):
        hex_result = "".join([format(val, '02X') for val in arg1])
        return hex_result
    

    The only thing you need to do now is import the test library in your robot file and call the keyword as you would do with an already existing one.

    *** Settings ***
    | Library | MyLibrary.py
    
    *** Test Cases ***
    | Example that get color in format RGB and transform it into HEX
    |  ${elements}  |  Get Webelement  |  (//div[@class="slds-col call-scripticon"])[1]
    |  ${bg color}  |  Call Method     |  ${elements}        |   value_of_css_property    | background-color 
    |  ${hex_color} |  transform RGB to HEX |  ${bg color}