Software Development and QA Tips By QASource Experts

How Do You Handle a Broken Image in Selenium?

Written by Dapheny Murphy | Jan 2, 2023 5:00:00 PM

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