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

}
}

6 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. 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

    ReplyDelete
  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

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

    ReplyDelete
  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

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

    ReplyDelete