Java Selenium WebDriver 中十六进制格式的 getCssValue (Color)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19668893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:16:29  来源:igfitidea点击:

getCssValue (Color) in Hex format in Selenium WebDriver

javaseleniumselenium-webdriver

提问by user2410266

In the following code I need to print the colorin Hex format.

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

FirstPrint statement is showing value in RGBformat which is rgb(102,102,102).

首先打印语句显示值RGB的格式是rgb(102,102,102)

The Secondstatement is showing value in Hexwhich is #666666

第二个语句显示值在Hex其中#666666

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

但是我手动将值输入到第二个打印语句中,即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);
    }
}

采纳答案by Ripon Al Wasim

Way 1: Using StringTokenizer:

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

回答by Yi Zeng

First a quote from Selenium's documentation.

首先引用 Selenium 的文档。

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.

获取给定 CSS 属性的值。颜色值应作为 rgba 字符串返回,因此,例如,如果在 HTML 源代码中将“background-color”属性设置为“green”,则返回值将为“rgba(0, 255, 0, 1)”。请注意,根据 DOM CSS2 规范,不会返回速记 CSS 属性(例如背景、字体、边框、边框顶部、边距、边距顶部、填充、填充顶部、列表样式、轮廓、暂停、提示) - 您应该直接访问普通属性(例如背景颜色)以访问所需的值。

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.

那么这不是一个 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);

回答by Victor Moraes

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

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

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

回答by Fai Hasan

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

该代码有效,但只是一个小错误。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);