Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getCssValue (Color) in Hex format in Selenium WebDriver

In the following code I need to print the color in Hex format.

First Print statement is showing value in RGB format which is rgb(102,102,102).

The Second statement is showing value in Hex which is #666666

But I am manually entering the value into the second print statement which is 102,102,102.

Is there any way to pass the value which I got from the 1st statement (Color) into the second print statement and get result?

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Google {

    public static void main(String[] args) throws Exception {

        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com/");
        String Color = driver.findElement(By.xpath("//div[@class='gb_e gb_f gb_g gb_xb']/a")).getCssValue("color");
        System.out.println(Color);
        String hex = String.format("#%02x%02x%02x", 102,102,102);
        System.out.println(hex);
    }
}
like image 358
user2410266 Avatar asked Oct 29 '13 20:10

user2410266


4 Answers

I know this is rather old, but you can get a simpler solution by using org.openqa.selenium.support.Color:

import org.openqa.selenium.support.Color;
String color = driver.findElement(By.xpath("//div[@class='gb_e gb_f gb_g gb_xb']/a")).getCssValue("color");
System.out.println(color);
String hex = Color.fromString(color).asHex();
System.out.println(hex);

It gives you a single line solution and even adds leading zeroes when required (something that the previous answers aren't accounting for)

like image 169
Victor Moraes Avatar answered Oct 10 '22 16:10

Victor Moraes


The code works, but just a little typo. The Color.fromString would be upper case C

import org.openqa.selenium.support.Color;

String color = driver.findElement(By.xpath("xpath_value")).getCssValue("color");
System.out.println(color);
String hex = Color.fromString(color).asHex();
System.out.println(hex);
like image 28
Fai Hasan Avatar answered Oct 10 '22 18:10

Fai Hasan


Way 1: Using StringTokenizer:

String color = driver.findElement(By.xpath("//div[@class='gb_e gb_f gb_g gb_xb']/a")).getCssValue("color");
String s1 = color.substring(4);
color = s1.replace(')', ' ');
StringTokenizer st = new StringTokenizer(color);
int r = Integer.parseInt(st.nextToken(",").trim());
int g = Integer.parseInt(st.nextToken(",").trim());
int b = Integer.parseInt(st.nextToken(",").trim());
Color c = new Color(r, g, b);
String hex = "#"+Integer.toHexString(c.getRGB()).substring(2);
System.out.println(hex);

Way 2:

String color = driver.findElement(By.xpath("//div[@class='gb_e gb_f gb_g gb_xb']/a")).getCssValue("color");
String[] numbers = color.replace("rgb(", "").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);
like image 42
Ripon Al Wasim Avatar answered Oct 10 '22 18:10

Ripon Al Wasim


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);
like image 23
Yi Zeng Avatar answered Oct 10 '22 17:10

Yi Zeng