How to verify color of a web element in Selenium Webdriver

0 votes
How to verify color of a web element in Selenium Webdriver?
Jul 3, 2019 in Selenium by Nisha
17,627 views

2 answers to this question.

0 votes

@Hello Nisha, you can discover the color of a web element and then verify it by using getCssValue() method, which take CSS attribute as input and return value of that attribute like color, font-size etc. Below is a sample code for verifying the color of a web element:

public void GoogleAbout() throws Exception{

String color = driver.findElement(By.xpath("//a[@href='products/']")).getCssValue("color");
String[] hexValue = color.replace("rgba(", "").replace(")", "").split(",");

int hexValue1=Integer.parseInt(hexValue[0]);
hexValue[1] = hexValue[1].trim();
int hexValue2=Integer.parseInt(hexValue[1]);
hexValue[2] = hexValue[2].trim();
int hexValue3=Integer.parseInt(hexValue[2]);

String actualColor = String.format("#%02x%02x%02x", hexValue1, hexValue2, hexValue3);

Assert.assertEquals("#245dc1", actualColor);
}

Hope it helps!

Check out the Selenium Online Course to learn more about Webdriver.

answered Jul 3, 2019 by Purnima
0 votes
document.getElementsByTagName('div')[0].style.backgroundColor
answered Aug 31, 2020 by Sri
• 3,190 points

Related Questions In Selenium

0 votes
2 answers

How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver ?

JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy("X", " y ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
6,159 views
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,762 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,184 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,258 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,895 views
0 votes
1 answer

How to mouse hover on a web element using Selenium WebDriver?

Hello Nitin, to automate mouse hovering over ...READ MORE

answered May 7, 2019 in Selenium by Anvi
• 14,150 points
6,553 views
0 votes
1 answer

How to drag a web element using Selenium Webdriver?

Hey @Kritika, you can use Actions class ...READ MORE

answered Jul 2, 2019 in Selenium by Ashwin
3,365 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP