Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

How to extract CSS properties of an element: Interview question Selenium

During functional test script creation using selenium , we came across steps like verify the background color , font size or the text color of the element. These are the different CSS properties associated with an element. We can extract the value of CSS properties using the below code:

What we require is to:
  • Identify the element 
  • Get CSS Attribute on the element
  • Store the property in a variable

WebElem = driver.findElement(By.id("testbhd"));

// to get background color
String bgColor = WebElem.getCssValue("background-color");

// to get text color
String bgColor = WebElem.getCssValue("color");

// to get font size
String bgColor = WebElem.getCssValue("font-size");