Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Selenium by (7k points)

I do want to check the color of an element in an html page

I tried to get color of the element as follow: 

WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("Ab_banco_M1T1_switch")));

element.getAttribute("background")

element.getAttribute("style")

element.getAttribute("background-color") 

element.getCssValue("style")

element.getCssValue("color")

But they return null or the backgorund-color of the page.

1 Answer

0 votes
by (31.9k points)

You can get the background color of element using:

element.getCssValue("background-color");

You can get the color of element text/caption by:

element.getCssValue("color");

Example:

If you want to get background and text color of "Sign in" button for LinkedIn, the code is :

driver.get("https://www.linkedin.com/");

        String buttonColor = driver.findElement(By.name("submit")).getCssValue("background-color");

        String buttonTextColor = driver.findElement(By.name("submit")).getCssValue("color");

        System.out.println("Button color: " + buttonColor);

        System.out.println("Text color " + buttonTextColor);

Want to learn Selenium, checkout our Selenium training.

Browse Categories

...