How to verify text color in selenium webdriver?
46,086
Solution 1
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.
Solution 2
You could use .getCssValue
to get the value of color.
As you specified, if you want to verify the color, you can assert it, something like this,
assertTrue(selenium.isElementPresent("css=td[bgcolor=#000]"));
Solution 3
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
![Sagar007](https://i.stack.imgur.com/p64yX.jpg?s=256&g=1)
Author by
Sagar007
Hello everyone I am working as Software Test Engineer at Flipkart , Bangalore. I have more than 3 plus exp in Automation and Manual Testing with Mobile testing as well. Contact mail : [email protected] Profile : http://stackoverflow.com/cv/qa.sagar
Updated on April 19, 2020Comments
-
Sagar007 about 4 years
I have a web application.
Written automation script with 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?
-
Sagar007 almost 10 yearsActually it has no CSS value. It is giving color after selection.
-
Vignesh Paramasivam almost 10 yearsIs it a link text or what? please give more information
-
Sagar007 almost 10 yearsIt is simple text. But now I want to check it`s colour code.
-
Sagar007 almost 10 yearsI will try your solution.If I get success to find it`s CSS.