How Do You Handle a Broken Image in Selenium?

Dapheny Murphy | January 2, 2023

How Do You Handle a Broken Image in Selenium?

A broken image is a link that does not show up as a picture. By clicking on it, the end-user is redirected to a distorted picture and a 404 error notification. This error means that the image is not loading properly. There can be several reasons for this like incorrect image format or URL, or server issues.

Steps for fixing broken images in Selenium:

  • Open the browser and find all the images using <img> attribute with the findElements method
  • Iterate on each image
  • Use the get method on the image and retrieve its status code
  • Assert whether the status code is 200, if it is 200, it is fine, if not, then it is a broken image

Disclaimer

This publication is for informational purposes only, and nothing contained in it should be considered legal advice. We expressly disclaim any warranty or responsibility for damages arising out of this information and encourage you to consult with legal counsel regarding your specific needs. We do not undertake any duty to update previously posted materials.

Post a Comment

Categories