Selenium: how to getCssValue with Selenium WebDriver

Last updated on:

From time to time you would need to getCssValue of particular element on the page. For example in my current project when I click on tab it is changing background color, so whenever tab is active I'm asserting that background-color for particular tab changed. Here is how you do it:

WebElement cssElement = driver.findElement(By.id("elementID"));
assertEquals("rgba(243, 242, 235, 1)", cssElement.getCssValue("background-color"));
assertEquals("238px", cssElement.getCssValue("width"));
assertEquals("absolute", cssElement.getCssValue("position"));
assertEquals("0.9", cssElement.getCssValue("opacity"));
View Comments

Leave a Comment

Use markdown or basic HTML and be nice.