Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Method 2: getCssValue() : What, When and How to use?

Posted on 03/16/2025 By admin

Hello Folks,

In last post, we have seen about getAttribute(). In this post we will learn about getCssValue() method. This post covers:

  1. What is getCssValue() method?
  2. Why we use getCssValue() method?
  3. How to use getCssValue() method?
  4. What is difference between getAttribute() and getCssValue() methods.

Let’s start with basics.

What is CSS:

Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language. It describes how HTML elements are to be displayed on screen, paper, or in other media. It describes look and feel of HTML elements(font size, width, height etc) i.e. how should it be displayed. It provides special and easy ways to specify various properties to HTML element. We can specify several style properties for a HTML element.

Example: 

Hey, There!!

In above example, “style” attribute is way of declaring CSS properties of any HTML element. Each attribute has a name and a value, separated by a colon (:). Each property declaration is  separated by a semi-colon (;). In above example, bold texts are CSS properties. For more details read my old post here.

Why we need getCssValue() method?

By using getAttribute() method we can retrieve value of attributes which are written as:

attributeName=”attributeValue”

So, getAttribute() can give you all css properties defined for html element at once but not individually. For example, if you want to retrieve only font size or color, you can not retrieve through getAttribute() method.

Take example of Search box available on Google:

CSS properties are defined from Styles pan as well. To understand how CSS properties are defined, refer this link.

JAVA program to retrieve all CSS properties through getAttribute() method:


package MakeSeleniumEasy;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class GetCSSAttribute {

        public static void main(String[] args) {
                
                System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
                WebDriver driver= new ChromeDriver();
                driver.manage().window().maximize();
                driver.get("https://www.google.com");
                WebElement searchTextBox= driver.findElement(By.id("lst-ib"));
                String allCssProperties= searchTextBox.getAttribute("style");
                System.out.println("CSS Properties: "+allCssProperties);

        }
}

Output:
CSS Properties: border: none; padding: 0px; margin: 0px; height: auto; width: 100%; background: url(“data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D”) transparent; position: absolute; z-index: 6; left: 0px; outline: none;

But if you want to retrieve value of particular CSS property, you need to use getCssValue() method.

What is getCssValue() method?

It gets the value of a given CSS property.

Syntax:

java.lang.String getCssValue(java.lang.String propertyName)

There are predefined CSS properties which you can see here.

Note:

  1. Color values should be returned as rgba strings, so, for example if the “background-color” property is set as “green” in the HTML source, the returned value will be “rgba(0, 255, 0, 1)”.
  2. Shorthand CSS properties (e.g. background, font, border, border-top, margin, margin-top, padding, padding-top, list-style, outline, pause, cue) are not returned, in accordance with the DOM CSS2 specification .You should directly access the longhand properties (e.g. background-color) to access the desired values.

How to use getCssValue() method?

We can use getCssValue() method by passing longhand css property name.

JAVA CODE:


package MakeSeleniumEasy;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class GetCSSAttribute {

        public static void main(String[] args) {
                
                System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
                WebDriver driver= new ChromeDriver();
                driver.manage().window().maximize();
                driver.get("https://www.google.com");
                WebElement searchTextBox= driver.findElement(By.id("lst-ib"));
                String color= searchTextBox.getCssValue("background-color");
                System.out.println("Color is : "+color);
                String font= searchTextBox.getCssValue("font-size");
                System.out.println("Font is : "+font);
                driver.quit();
                
                
                

        }
}

Output: Color is : rgba(255, 255, 255, 1)

Font is : 16px

Uncategorized

Post navigation

Previous Post: TestNG Tutorials 69 : Rerun Failed Test Method Using IRetryAnalyzer Interface – Implementing With Listener – IAnnotationTransformer
Next Post: Frequently Asked Java Program 28: Java Program to Find if Given Number Is a Perfect Number

Related Posts

How To Select Random Value From Any Drop down In Selenium Web Driver Uncategorized
October 28, 2018 – Make Selenium Easy Uncategorized
June 15, 2018 – Make Selenium Easy Uncategorized
Postman Tutorial Part 22 – Postman Console – Debugging And Logging Uncategorized
CSS Selector In Selenium Webdriver: Points You Must Know Uncategorized
FileUpload – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark