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 2 in Selenium by Nisha
487 views

1 answer 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);
}
answered Jul 3 by Purnima

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

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,110 points
908 views
0 votes
1 answer
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,130 points
1,098 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
831 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 in Selenium by Anvi
• 13,960 points
325 views
0 votes
1 answer