Friday, 27 December 2013

Selenium Webdriver - Get Background color of an element in Hex - Example

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;

import java.util.*;
import java.lang.*;

import org.openqa.selenium.*;

public class class1 {
public static void main(String[] args) {
//Initiate Driver  
WebDriver driver = new FirefoxDriver();
String text;
WebElement WebElement1 = driver.findElement(By.xpath("//li[@id='no_transition']"));
text =WebElement1.getCssValue("background-color").toString();
//Split css value of rgb
String[] numbers = text.replace("rgba(", "").replace(")", "").split(",");
int number1=Integer.parseInt(numbers[0]);
numbers[1] = numbers[1].trim();
int number2=Integer.parseInt(numbers[1]);
numbers[2] = numbers[2].trim();
int number3=Integer.parseInt(numbers[2]);
String hex = String.format("#%02x%02x%02x", number1,number2,number3);



  1. Hi Madam,
    Why did you stop writing?I was searching for a simple Framework program using selenium webdriver.Your Keyword Driven Framework was good.Thanks :)

  2. I have definitely picked up anything new from right here. I did however expertise a few technical points using this site, since I experienced to reload the web site a lot of times previous to I could get it to load correctly.
    Office Interiors in Chennai

  3. This is a very interesting web page and I have enjoyed reading many of the articles and posts contained on the website, keep up the good work and hope to read some more interesting content in the future.
    Pest Control in Chennai
    Security Services in Chennai

  4. Really Good article.provided a helpful information.keep updating...
    E-mail marketing company in india

  5. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
    Android App Development Company
    iOS App Development Company

  6. you are posting a good information for people and keep maintain and give more update too.
    seo company in india

  7. Article is very different in the topic points and the way of writing.Keep updating more articles.

    Python Training in Chennai

  8. String color = driver.findElement(By.className("###")).getCssValue("background-color");

    String hex = Color.fromString(color).asHex();

    This is way easier

  9. The Mobile Accessories is a largest mobile retail Chain dealing in leading international and Indian Brands of mobile phones and accessories headquartered with using special offers and low cost of the latest branded mobile phones. This is amazing offers with some of days.

    Mobile Showrooms in OMR