In the following code I need to print the color
in Hex format
.
First Print statement is showing value in RGB
format
First a quote from Selenium's documentation.
Get the value of a given CSS property. Color values should be returned as rgba strings, so, for example if the "background-color" property is set as "green" in the HTML source, the returned value will be "rgba(0, 255, 0, 1)". Note that shorthand CSS properties (e.g. background, font, border, border-top, margin, margin-top, padding, padding-top, list-style, outline, pause, cue) are not returned, in accordance with the DOM CSS2 specification - you should directly access the longhand properties (e.g. background-color) to access the desired values.
Then this is not a Selenium specific question, this is just a general programming question about how to parse string rgba(102,102,102)
to three number.
// Originally untested code, just the logic.
// Thanks for Ripon Al Wasim's correction.
String color = driver.findElement(By.xpath("//div[@class='gb_e gb_f gb_g gb_xb']/a")).getCssValue("color");
String[] numbers = color.replace("rgba(", "").replace(")", "").split(",");
int r = Integer.parseInt(numbers[0].trim());
int g = Integer.parseInt(numbers[1].trim());
int b = Integer.parseInt(numbers[2].trim());
System.out.println("r: " + r + "g: " + g + "b: " + b);
String hex = "#" + Integer.toHexString(r) + Integer.toHexString(g) + Integer.toHexString(b);
System.out.println(hex);