use*_*266 8 java selenium selenium-webdriver
在下面的代码我需要打印color在Hex format.
First Print语句以RGB格式显示值rgb(102,102,102).
在第二个语句显示值在Hex 其中#666666
但是我手动将值输入到第二个print语句中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);
}
}
Run Code Online (Sandbox Code Playgroud)
Vic*_*aes 12
我知道这是相当陈旧的,但您可以通过以下方式获得更简单的解决方案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);
Run Code Online (Sandbox Code Playgroud)
它为您提供单线解决方案,甚至在需要时添加前导零(以前的答案不考虑)
方式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);
Run Code Online (Sandbox Code Playgroud)
方式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);
Run Code Online (Sandbox Code Playgroud)
首先引用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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26473 次 |
| 最近记录: |