Articles → SELENIUM → Check the color of a web element using getCssValue method in Selenium

Check the color of a web element using

getCssValue method in Selenium






Purpose of getCssValue





Syntax


element.getCssValue("property_name")





Scenario





Click to Enlarge


Code


package com.FirstSeleniumProject;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class MyClass {
    @Test
    public void TestAnnotation() {
        WebDriver driver;
        String service = "IEDriverServer path";
        System.setProperty("webdriver.ie.driver", service);
        driver = new InternetExplorerDriver();
        driver.get("https://www.codeproject.com/Articles/1170333/Customizing-ASP-NET-MVC-Bootstrap-Templates");

        WebElement element = driver.findElement(By.cssSelector("div#contentdiv > h2"));

        String color1[];
        color1 = element.getCssValue("color").replace("rgba(", "").split(",");
        String hex = String.format("#%02x%02x%02x", Integer.parseInt(color1[0].trim()), Integer.parseInt(color1[1].trim()), Integer.parseInt(color1[2].trim()));

        //System.out.println(hex.toUpperCase());
        Assert.assertEquals(hex.toUpperCase(), "#FF9900");

    }
}



Output



Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, April 19, 2017

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250