Use Javascript to get all css applied on webelement in selenium

Here I have written a small program which gets all the css of Webelement and prints it out in a java-selenium program. I have used JavascriptExecutor to execute the script. This can be used to match your test-data with css we get at runtime. So, this script can be used for accessibility testing.

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class ElementCssDemo {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();

// Navigate and login in Box Account


WebElement emailLoginfield = driver.findElement("login"));

//get css of webelement by applying following script with JavascriptExecutor

JavascriptExecutor executor = (JavascriptExecutor)driver;

String script = "var s = '';" +

                "var o = getComputedStyle(arguments[0]);" +

                "for(var i = 0; i < o.length; i++){" +

                "s+=o[i] + ':' + o.getPropertyValue(o[i])+';';}" + 

                "return s;";


//gets output with all css in String

String allCss = executor.executeScript(script, emailLoginfield).toString();

//splits the String into array to show every css separately.

String[] cssArray = allCss.split(";");

for(String str : cssArray){







QACult Best Software Testing Courses in Chandigarh tricity.- We love to enhance your knowledge.

QACult is the premier institute catering to the requirements of experienced and fresh pass-out that gives leaders like you a new way to experience Quality engineering—while you work and giving you the flexibility to both advance your career.
Our faculty have 12+ years of industrial experience and have developed many automation testing frameworks in java using TestNG or BDD (cucumber) methodology. We expertise in developing automation testing frameworks in java, python, javascript, php, ruby(WATIR-webdriver & Capybara) and Appium.
please subscribe our channel for more such updates:
And visit our website: for various blogs and Upcoming Events.