user3190414
user3190414

Reputation: 113

How to verify in selenium if the image is shown on the page?

I need to verify if a particular image is shown on page with selenium in python. HTML code looks like this when the image is displayed in a table cell:

<td role="gridcell">
<div style="text-align: center">
<img class="grid-center-image" border="0" ng-show="dataItem.device_protected" src="resources/images/icon_supported.png"/>
</div>
</td>

When the image is not displayed it looks like:

<td role="gridcell">
<div style="text-align: center">
<img class="grid-center-image ng-hide" border="0" ng-show="dataItem.device_protected" src="resources/images/icon_supported.png"/>
</div>
</td>

I do not understand how to approach this? How do I verify if the image is present on the screen?

Upvotes: 1

Views: 5125

Answers (2)

Florent B.
Florent B.

Reputation: 42518

To find out if the image icon_supported.png is hidden with a single call:

if driver.find_elements_by_css_selector("img[src$='icon_supported.png'].ng-hide") :
    print "image hidden"
else :
    print "image displayed"

Upvotes: 0

Hassan Mehmood
Hassan Mehmood

Reputation: 1402

You have to get class attribute of your img tag and then have to check that whether 'ng-hide' is present in it or not. That'll decide the presence of image on screen.

Below is the sample code in python to check visibility of image on screen.

elem = driver.find_element_by_xpath('//your/xpath/to/img/tag')
if 'ng-hide' in elem.get_attribute('class'):
    print 'Image is not visible on screen'
else:
    print 'Image is visible on screen'   

Upvotes: 1

Related Questions