I have read that there is no "sure shot" way of knowing that an image is loaded or displayed on a webpage using Selenium. I want to know the reason behind it, as to why this is completely not possible.
I tried three different kinds of checks and I see they all return the incorrect result, i.e. even if the image is broken, the result returns true.
driver.get('http://demo.tanmaysarkar.com/sample_04.html') image1 = driver.find_element_by_xpath("//img[contains(@alt,'image two')]") bool(driver.execute_script("return arguments.complete && typeof arguments.naturalWidth != \"undefined\" && arguments.naturalWidth > 0", image1)) if not bool: print('d') else: print('c')
This returns 'c' for image two on the given webpage, which should not be the case.
Using size attribute
if image1.size != 0 : print("good") else: print('food')
Again returns 'good', which I'm not expecting it to do. And finally, being captain obvious:
Using is_displayed( ) method
if image1.is_displayed(): print('foo') else: print('fii')
prints 'foo', which again is not expected.
I'm curious to know what else I can to do a sort of assert truly if the image is shown or not.
Can checking for a 404 error code help here? I am taking this from this example that I came upon - Broken Images check
Any help/suggestion/explanation would be appreciated.