3

I want to get the style value from an element and assign it to a string var. I tried using the below code but it returns a value of 'rgb(173, 255, 47)' when I'm trying to return the value 'background:#ADFF2F"':

IWebElement highlightedText = _driver.FindElement(By.Id("firstHeading")).FindElement(By.CssSelector("th-highlight-span"));
actualHighlightColour = highlightedText.GetAttribute("style");

This is the src code from the web page I want to get the value of style from:

<h1 id="firstHeading" class="firstHeading" lang="en">
 ::before
 <th-highlight-span style="background:#ADFF2F" data- 
 rwstate="ss">California Proposition 218 (1996)</th-highlight-span>

The code I tried to use returns 'rgb(173, 255, 47)' which can be found here but I want to return background: #ADFF2F:

element.style {
background: #ADFF2F;
background-image: initial;
background-position-x: initial;
background-position-y: initial;
background-size: initial;
background-repeat-x: initial;
background-repeat-y: initial;
background-attachment: initial;
background-origin: initial;
background-clip: initial;
background-color: rgb(173, 255, 47);
4
  • So, do you have any error messages? Commented May 24, 2018 at 9:05
  • @RatmirAsanov no I don't get any error messages, I get rgb(173, 255, 47) returned and assigned to actualHighlightColour var but I want to return background:#ADFF2F ; Commented May 24, 2018 at 9:14
  • 1
    Try highlightedText.GetCssValue("background"); Commented May 24, 2018 at 9:25
  • @AmanB when I use that it returns the following "rgb(173, 255, 47) none repeat scroll 0% 0% / auto padding-box border-box" Commented May 24, 2018 at 9:29

2 Answers 2

1

Convert RGB to hex format

 String hex = String.format("#%02x%02x%02x", r, g, b);

This will return the hex in small alphabet and if want this in caps then simply replace x with X

Sign up to request clarification or add additional context in comments.

Comments

1

As per the HTML you have shared to retrieve the style attribute i.e. the text background:#ADFF2F you can use the following solution :

actualHighlightColour = _driver.FindElement(By.CssSelector("h1.firstHeading#firstHeading>th-highlight-span")).GetAttribute("style");

Update

As the above expression still returns background: rgb(173, 255, 47);, get the output as rgb(173, 255, 47) you can use the GetCssValue() method and you can use the following solution:

actualHighlightColour = _driver.FindElement(By.CssSelector("h1.firstHeading#firstHeading>th-highlight-span")).GetCssValue("background");

You can find a detailed discussion in How to convert #ffffff to #fff or #fff to #ffffff while asserting the background color rgb(255,255,255) returned by Selenium getCssValue(“background”)

2 Comments

I tried this but it returns "background: rgb(173, 255, 47);" and not background:#ADFF2F
@Tester_Giant1 Can you try the updated answer and let me know the status please?

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.