【问题标题】:getCssValue (Color) in Hex format in Selenium WebDriverSelenium WebDriver中十六进制格式的getCssValue(颜色)
【发布时间】:2013-11-09 05:42:42
【问题描述】:

在下面的代码中,我需要在Hex format 中打印color

第一个打印语句以RGB 格式显示值,即rgb(102,102,102)

Second 语句在 Hex 中显示值,即 #666666

但我手动将值输入到第二个打印语句 102,102,102

有什么方法可以将我从第一个语句(颜色)得到的值传递到第二个打印语句并得到结果?

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);
    }
}

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

    首先引用 Selenium 的文档。

    获取给定 CSS 属性的值。应返回颜色值 作为 rgba 字符串,例如,如果“背景颜色”属性是 在 HTML 源代码中设置为“green”,返回值将是“rgba(0, 255, 0, 1)"。请注意,速记 CSS 属性(例如背景、 字体,边框,边框顶部,边距,边距顶部,填充,填充顶部, list-style, outline, pause, cue) 不返回,按照 DOM CSS2 规范 - 您应该直接访问速记 属性(例如背景颜色)来访问所需的值。

    那么这不是 Selenium 特定的问题,这只是一个关于如何将字符串 rgba(102,102,102) 解析为三个数字的一​​般编程问题。

    // 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);
    

    【讨论】:

    • user1177636:您之前的代码是 Integer.toHexString(),但参数应该是整数,如 Integer.toHexString()
    【解决方案2】:

    方式一:使用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);
    

    方式二:

    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);
    

    【讨论】:

    • 在“方式 2”中,您只使用 numbers[0],而我认为您实际上应该使用 numbers[0]numbers[1]numbers[2]
    • 感谢 Victor Moraes 强调错误。是的,应该是 numbers[0]、numbers[1] 和 numbers[2]。我已经相应地更正了。
    • "Way 2":十六进制值很可能应该用前导 0 填充。我在这里是因为 driver.getElement().getCssValue("background-color") 返回和 rgba 值:rgba(198, 15, 19, 1)。 DevTools 给出一个十六进制值:#c60f13,上面的代码返回#c6f13。建议String hex = String.format("#%02x%02x%02x", r,g,b);
    【解决方案3】:

    我知道这已经很老了,但您可以使用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);
    

    它为您提供单行解决方案,甚至在需要时添加前导零(以前的答案没有考虑到这一点)

    【讨论】:

      【解决方案4】:

      代码有效,但只是一个小错误。 Color.fromString 将是大写 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);
      

      【讨论】:

      • 我认为我的答案中的一个字母拼写错误不会成为为此添加另一个答案的理由。像你一样编辑它。另外,当您说“代码有效,但只是有点错字”时,不清楚您在谈论哪个代码。我知道您指的是我的,因为我收到了通知,但对于新手来说可能不是那么清楚。如果您坚持提供单独的答案,为了清楚起见,我建议您添加一个指向我的链接。顺便说一句,现在你编辑了我的答案,你的只是我的一个副本,没有什么可添加的(它甚至已经过时了,因为我的不再有错别字了)
      • 谢谢兄弟,我无法编辑或评论,因为我未满 15 岁
      猜你喜欢
      • 1970-01-01
      • 2014-07-03
      • 2016-02-09
      • 2019-06-16
      • 2021-03-08
      • 2013-10-28
      • 2019-10-29
      • 1970-01-01
      • 2020-03-05
      相关资源
      最近更新 更多