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 org.openqa.selenium.support.*;

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();
driver.get("http://www.w3.org/Talks/2012/0416-CSS-WWW2012/Demos/transitions/demo-transitions-1.html#no_transition");
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);
System.out.println(hex);

}
}

2 comments:

  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 :)

    ReplyDelete
  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

    ReplyDelete