Andremoniy
Andremoniy

Reputation: 34900

How to derive colour of text in element using Selenium?

Is it possible to derive (i.e. by automatically applying all inherited CSS styles and, if it is possible - JS scripts as well) colour of the text in given WebElement?

For instance, I want to detect colour of text in each visible element:

WebElement body = driver.findElement(By.tagName("body"));
List<WebElement> elements = body.findElements(By.cssSelector("*"));
for (WebElement we : elements) {
    if (we.isDisplayed()) {
       // ... colour of text in "we" element?
    }
}

Is it possible to do?

P.S. I realise that each we element can contain nested elements and colour inside them can differ. But let's simplify the issue and consider each we as element without nested tags.

Upvotes: 1

Views: 108

Answers (1)

pradeep1991singh
pradeep1991singh

Reputation: 8375

You can use getCssValue() something like below:

element.getCssValue("color")

which in your case will be

we.getCssValue("color")

For reference - http://www.seleniumeasy.com/selenium-tutorials/how-to-get-css-values-using-webdriver

Upvotes: 3

Related Questions