- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to verify color of a web element in Selenium Webdriver?
We can verify the color of a webelement in Selenium webdriver using the getCssValue method and then pass color as a parameter to it. This returnsthe color in rgba() format.
Next, we have to use the class Color to convert the rgba() format to Hex. Let us obtain the color an element highlighted in the below image. The corresponding color for the element is available under the Styles tab in Chrome browser.
The color of the element is also provided in the hex code #797979.
Syntax
WebElement t = driver.findElement(By.tagName("h1")); String s = t.getCssValue("color"); String c = Color.fromString(s).asHex();
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.support.Color; public VerifyColor{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.tutorialspoint.com/about/about_careers.htm"); // identify text WebElement t = driver.findElement(By.tagName("h1")); //obtain color in rgba String s = t.getCssValue("color"); // convert rgba to hex String c = Color.fromString(s).asHex(); System.out.println("Color is :" + s); System.out.println("Hex code for color:" + c); driver.quit(); } }
Output
- Related Articles
- How to verify the color and background color of a web element in Selenium?
- How to Verify Tooltip using Selenium WebDriver?
- How to verify an attribute is present in an element using Selenium WebDriver?
- How to use Selenium WebDriver for Web Automation?
- How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver?
- How to click on hidden element in Selenium WebDriver?
- How can I scroll a web page using selenium webdriver in python?
- Why use Selenium WebDriver for Web Automation?
- How can I verify Error Message on a webpage using Selenium Webdriver?
- Finding an element in a sub-element in Selenium Webdriver.
- How to Resolve Stale Element Reference Exception in Selenium WebDriver?
- How to get an attribute value of an element in Selenium Webdriver?
- How to obtain the tagname of the parent element in Selenium webdriver?
- How to scroll to element with Selenium WebDriver using C#?
- How to set “value” to input web element using selenium?

Advertisements