We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Retrieve computed CSS properties like color, font-size, and background.
In Selenium, locators find the element, and getCssValue() tells you how that element is styled on screen.
getCssValue(propertyName) returns the computed CSS value of a WebElement after all styles are applied.
getCssValue("background-color") → Returns final color as rgba(...)getCssValue("font-size") → Returns size like "16px"getCssValue("display") → Returns layout valuergba(), not hexElements:
id="style-btn" (blue background, white text)id="inspect-css" (shows computed CSS values)Click "Inspect CSS" to see the computed values that Selenium would read!
123456789WebElement button = driver.findElement(By.id("style-btn"));
String bgColor = button.getCssValue("background-color");
System.out.println("Background: " + bgColor);
String fontSize = button.getCssValue("font-size");
System.out.println("Font Size: " + fontSize);
driver.quit();"rgba(37, 99, 235, 1)"."16px").Even if CSS uses hex, Selenium returns rgba(). Compare logically, not by exact string match.
Focus on checks that reflect a state (error vs success) rather than every cosmetic detail.
Use id for styling checks instead of complex CSS/XPath.