Selenium WebDriver中以hex格式的getCssValue(Color)

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

First Print语句以RGB格式显示值rgb(102,102,102)

第二个语句显示Hex中的值,即#666666

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

有没有办法将我从第一个语句(Color)获得的值传递给第二个print语句并得到结果?

 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:

 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的文档。

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

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

我知道这是相当陈旧的,但您可以使用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); 

它为您提供单线解决方案,甚至在需要时添加前导零(以前的答案不考虑)

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