Selenium WebDriver中以十六进制格式的getCssValue(Color) [英] getCssValue (Color) in Hex format in Selenium WebDriver

查看:142
本文介绍了Selenium WebDriver中以十六进制格式的getCssValue(Color)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我需要以 Hex格式打印颜色

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

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

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

Second 语句显示 Hex中的值这是#666666

但我手动输入第二次打印的值语句 102,102,102

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

有没有办法传递我从第一个语句中获得的值(颜色) )进入第二个print语句并得到结果?

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


推荐答案

方式1:使用StringTokenizer:

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

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

这篇关于Selenium WebDriver中以十六进制格式的getCssValue(Color)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆