getCssValue (Color) in Hex format in Selenium WebDriver

前端 未结 4 1923
遥遥无期
遥遥无期 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)

提交回复
热议问题