getCssValue (Color) in Hex format in Selenium WebDriver

前端 未结 4 1909
遥遥无期
遥遥无期 2020-12-19 03:03

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

First Print statement is showing value in RGB format

相关标签:
4条回答
  • 2020-12-19 03:10

    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)

    0 讨论(0)
  • 2020-12-19 03:21

    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);
    
    0 讨论(0)
  • 2020-12-19 03:27

    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);
    
    0 讨论(0)
  • 2020-12-19 03:28

    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);
    
    0 讨论(0)
提交回复
热议问题