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




Picture showing the webportal from where the color would be retrieved using getCSSValue method
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


Picture showing the output of getCssValue method in Selenium
Click to Enlarge


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

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250