Intellipaat 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.

2 Answers

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.

0 votes
by (2.8k points)

You can grab the element’s color(Background color of element) by:
element.getCssValue("background-color");
You can grab the element’s text/caption color using:
element.getCssValue("color");
Like for example in order to get the background and text color of "Sign in" button for Facebook,the following code can be used:
driver.get("https://www.facebook.com/");
String button_Colour = driver.findElement(By.name("submit")).getCssValue("background-colour"); String buttonTextColor = driver.findElement(By.name("submit_button")).getCssValue("colour"); System.out.println("Button color: " + buttonColor); System.out.println("Text_color " + buttonTextColor);

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...