Can anybody help me about how to use these two functions to get value of any CSS property.
Asked
Active
Viewed 3.4k times
3
-
What seems to be the problem? [`WebElement.getCssValue()`](http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#getCssValue%28java.lang.String%29) returns a value of the element's CSS attribute. `String bgColor = driver.findElement(By.id("myId")).getCssValue("background-color");` – Petr Janeček Jun 11 '12 at 07:26
-
1http://www.whathaveyoutried.com - read these please. Context would help here. Post what you've tried, otherwise we've not got a clue what you are asking. – Arran Jun 11 '12 at 21:15
-
Thanks For ur concern Slanec and Arran Actually I wanted to check if any Label is Bold or not .For that I tried to check CSS property(font-weight: bold).But I was not able to use these funtions in eclipse(testNG) as my class is already extending one class.So Please suggest upon 1.How to use WebElement.getCssValue & WebElement.getAttribute in Eclipse(TestNG) 2.Can font verification(Bold) be done with these two Functions. – Virendra Joshi Jun 14 '12 at 06:01
-
Have a look here http://stackoverflow.com/questions/18452790/how-to-using-webdriver-selenium-to-get-the-value-of-style-element/20065390#20065390 – Anirudh Oct 27 '14 at 06:50
1 Answers
5
If have a particular <img>
tag as below
<img title="Title" alt="myTitle" src="A/B/C/xyz.png">
driver.getElement(By.tagName("img")).getAttribute("src")
will get you the src
attribute of this tag. Result - A/B/C/xyz.png
Similarly, you can get the values of attributes such as title
, alt
etc.
Similarly you can get CSS properties of any tag by using getCssValue("some propety")

Petr Janeček
- 37,768
- 12
- 121
- 145

Hari Reddy
- 3,808
- 4
- 33
- 42
-
How can we get the attribute using selenium rc rather than WebDriver – Harshavardhan Konakanchi Jun 26 '12 at 05:44
-
Maybe this question might help you - http://stackoverflow.com/questions/7201337/how-to-get-attribute-value-using-selenium-and-css – Hari Reddy Jun 26 '12 at 07:36
-
I need to get the attribute of the css class but not the html element.Refer to this link http://stackoverflow.com/questions/9578900/get-css-property-values-using-selenium – Harshavardhan Konakanchi Jun 26 '12 at 11:28