2

The x-path is like < img id="" src="/A/A/B.png" style="">

I am required to find the color of the image , the style does not contain anything about color. It contains information only about height,width.

I am sure in the src path the fill attribute would be present.But how am I suppose to get till that?

Here is what i think of doing:

String css = driver.findElement(By.Xpath("..blah../img").getCSSValue("background-color");

but i get 'transparent' as answer .How do i get over it?

Krish Krishna
  • 317
  • 3
  • 15
  • 1
    As per your question, i think you are trying to retrieve the background color of image, if so, there is no way of you retrieving the background color of image(if there is no background color), the return type would transparent, if i didn't get your question correctly, please be more specific – Rakesh Apr 07 '15 at 10:07
  • There is an image, but it is partly transparent, partly green in color....but the x-path given above will highlight both transparent as well as the green part.I tried to find more precise xpath but in vain.So i am trying to find out some more specific way of getting the color of img present only – Krish Krishna Apr 08 '15 at 08:23

1 Answers1

2

This will definetely works:

driver.findElement(By.xpath("..blah../img")).getCssValue("background-color").toString();

The problem is either your image is really transparent or color is defined in its parent.

Stan E
  • 3,396
  • 20
  • 31
  • Thanks for the reply!, Could you please elaborate about the parent part. Is it like the parent is the src?? – Krish Krishna Apr 08 '15 at 08:20
  • No, I meant that background could be made not within this tag, but using some of the parent elements, which is defining the background color for all the sub elements. – Stan E Apr 08 '15 at 08:23
  • The parent elements have no attributes related to the color of img, this image is completely from a different source(I can say for sure).... – Krish Krishna Apr 08 '15 at 08:34
  • Then are you sure that the background is not transparent? Is it possible to see what are you checking? Can you also see the css attributes in Chrome dev tools? – Stan E Apr 08 '15 at 08:35
  • I was told to be confidential about the information..:( . I have been using Firepath to check the attributes and stuff..,searched in style attribute, but no where i can see the color.... – Krish Krishna Apr 08 '15 at 08:41
  • Then, it's the thing, there is no background color, and it's transparent (translucent) – Stan E Apr 08 '15 at 08:42
  • I think that would be fine. – Krish Krishna Apr 08 '15 at 08:50
  • Do you mean the color of the picture by itself? Is it multi-color or just one? – Stan E Apr 08 '15 at 08:51
  • So, you should make a screenshot of specific element (http://stackoverflow.com/questions/13832322/how-to-capture-the-screenshot-of-only-a-specific-element-using-selenium-webdrive) and then check the color with something like http://pastebin.com/uXfz3GE0 (I'm checking the first pixel here) – Stan E Apr 08 '15 at 08:55
  • Does help.. I will check and let you know.! Thanks! – Krish Krishna Apr 08 '15 at 09:01