Java 如何验证 selenium webdriver 中的文本颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24669787/
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-14 13:59:10  来源:igfitidea点击:

How to verify text color in selenium webdriver?

javaselectcolorsselenium-webdrivermouseevent

提问by Sagar007

I have a web application.

我有一个网络应用程序。

Written automation script with selenium webdriver.

使用 selenium webdriver 编写自动化脚本。

I have write some color code when I select some text.

当我选择一些文本时,我已经写了一些颜色代码。

Now I want to check that color is present or not.

现在我想检查颜色是否存在。

How can I verify color code in selenium webdriver script?

如何验证 selenium webdriver 脚本中的颜色代码?

采纳答案by Sagar007

After so many try with different script finally I am able to find my question`s answer.

经过多次尝试不同的脚本后,我终于找到了我的问题的答案。

String colorString = driver.findElement(By.id("foo")).getAttribute("class");
String[] arrColor = colorString .split("#");
assertTrue(arrColor[1].equals("FFFFFF"));

Thank You all for helping me.

谢谢大家帮助我。

回答by Vignesh Paramasivam

You could use .getCssValueto get the value of color.

您可以使用.getCssValue来获取颜色的值。

As you specified, if you want to verify the color, you can assert it, something like this,

正如你所指定的,如果你想验证颜色,你可以断言它,像这样,

assertTrue(selenium.isElementPresent("css=td[bgcolor=#000]"));

回答by Naga Tirumalasetti

//you can also try converting rgb into hex and verify...

// Color RGB   
 color = driver.findElement(By.id("xxxxx")).getCssValue("color").trim();    
 System.out.println("RGB_Color: " + color);  

 //  RGB to HEX   
 String color_hex[];  
 color_hex = color.replace("rgba(", "").split(",");       
 String actual_hex = String.format("#%02x%02x%02x", Integer.parseInt(color_hex[0].trim()), Integer.parseInt(color_hex[1].trim()), Integer.parseInt(color_hex[2].trim()));  

// further can verify with Actual hex value with Expected hex value  
Assert.assertEquals("actual_hex should equal to: ", "#808080", actual_hex);

回答by Tapan Khimani

Green color hash code #RRGGBB : #008000// Test Data, can replace with any specific color

绿色哈希码 #RRGGBB : #008000// 测试数据,可以替换为任何特定颜色

Can try below code :

可以试试下面的代码:

String colorString = driver.findElement(By.id("foo")).getAttribute("class");
String[] arrColor = colorString .split("#");
assertTrue(arrColor[1].equals("008000"));

回答by Satya Sunil

WebElement eleSearch = driver.findElement(By.xpath("//*[@class='navsearchbar']//div[2]//div"));

String rgbFormat = eleSearch.getCssValue("background-color");

System.out.println(rgbFormat);     //In RGB Format the value will be print => rgba(254, 189, 105, 1)

String hexcolor = Color.fromString(rgbFormat).asHex(); //converted Into HexFormat
System.out.println(hexcolor);// Output of Hex code will be  => #febd69