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. Robotic Process Automation (RPA) is one of the most exciting developments in Business Process Management (BPM) in recent history. Some industry experts believe it may be even more transformational than cloud computing transformational than cloud Automationminds team. (RPA)Automationminds lets you program in (RPA),
    Robotic Process Automation Anywhere and course bluePrism

    ReplyDelete