javaselenium-webdriver

Getting the colors of elements using Selenium Web Driver


I have an element whose border is one color and its fill is another. I want to get the color of both.

I tried "color" and "border-color" arguments in getCssValue(). However, I get only the border color.

How do I get them?


Solution

  • getCssValue() only returns the value of a single CSS response. You may need invoke that twice to get that data

    Example Java Code Part:

                WebElement element = driver.findElement(By.cssSelector("whatever you want"));
    
                String backgroundColor = element.getCssValue("background-color");
                String borderColor = element.getCssValue("border-color");
    
                System.out.println("Bg Color: " + backgroundColor);
                System.out.println("Border Color: " + borderColor);
    

    As another approach; you may use XPath if you are familiar with them.