2
<div x="10" y="6" height="12"  fill="#F89406">

How can i try to identify the color from the above html in selenium driver using java.

The output should be like orange( because #F89406 is orange)

What i think of doing is to assign color to variables in an array and depending on the color code i would like to compare, but it is becoming too long.

Krish Krishna
  • 317
  • 3
  • 15
  • @ krish : How many colors you expect would come under div. If they are less then you can easily assign them their respective colors and pick the one but if they are more then lets try using some pre-defined functions. – Rupesh Shinde Mar 31 '15 at 13:56
  • @Rupesh: Thanks for the reply! Actually I was looking for some predefined functions which would directly give the color once we are passing the color code.... – Krish Krishna Mar 31 '15 at 14:08
  • Inaddition to that the requirement is like there are diiferent elements in the page so writing a varible for each color would be difficult(there are total 220+ colors) – Krish Krishna Mar 31 '15 at 14:09

1 Answers1

0

There does not seem to be any proper API that will get you the color name from your hex code. One thing you can do is to write a script that will query a website like - http://name-of-color.com/ with the hex code and parse the html code and retrieve the Color name that is displayed on the website.

So, overall the steps would be:

  1. Using selenium get the hex code of the color using below xpath

    driver.findElement(By.xpath("//div[@fill]").getAttribute("fill").getText();
    
  2. Query the website as follows (you could use the URL, URLConnection class for this OR you could use Selenium+PhantomJS OR Jsoup)

    URL: http://name-of-color.com/?hex=#F89406

  3. Scrape the html page source and get the color name that is displayed on the website

LittlePanda
  • 2,496
  • 1
  • 21
  • 33