How to verify the color and background color of a web element in Selenium?


We can verify the color and background color of a web element in Selenium with the help of getCSSValue() method.

Example

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class CssColorValue {
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://www.tutorialspoint.com/index.htm";
      driver.get(url);
      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      //getting color attribute with getCssValue()
      String colr = driver.findElement(By.xpath("//*[text()=’GATE Exams’]"))
      .getCssValue("color");
      //getting background color attribute with getCssValue()
      String bckgclr = driver.findElement(By.xpath("//*[text()=’ ’GATE Exams’]"))
      .getCssValue("background-color");
      System.out.println(colr);
      System.out.println(bckgclr);
      driver.close();
   }
}

Updated on: 11-Jun-2020

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements