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. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.

    Restaurant in OMR
    Apartments in OMR
    Villas in OMR
    Resorts in OMR

  3. This comment has been removed by the author.

  4. There is a better way to optimize this. try this code simple
    text =WebElement1.getCssValue("background-color");
    String hex=Color.fromString(text).asHex();

  5. I’m not sure where you’re getting your information, but good topic. I need to spend some time learning more or understanding more. Thanks for fantastic info I was looking for this information for my mission.
    Paper Publishing Sites
    Naas Rated Journals
    Language Translation Services
    Research Paper Writing Service
    Article Writing Services

  6. I have really happy to these reading your post. This product control and maintenance of our health.The daily routine can assist you weight lose quickly and safely.My life is completely reworked once I followed this diet.I feeling nice concerning myself.

    Herbalife in Chennai
    Nutrition centers in Chennai
    Weight Loss in Chennai
    Weight Gain in Chennai