Find broken images in page & image replace by another image

前端 未结 3 2084
说谎
说谎 2021-02-14 02:46

Hi I am using selenium webdriver 2.25.0 & faceing the some serious issues,

  1. how to find broken images in a page using Selenium Webdriver
  2. How to find th
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-14 03:07

    The accepted answer requires that you use a proxy with an extra call to each image to determine if the images are broken or not.

    Fortunately, there is another way you can do this using only javascript (I'm using Ruby, but you can use the same code in any executeScript method across the WebDriver bindings):

    images = @driver.find_elements(:tag_name => "img")
    broken_images = images.reject do |image|
      @driver.execute_script("return arguments[0].complete && typeof arguments[0].naturalWidth != \"undefined\" && arguments[0].naturalWidth > 0", image)
    end
    # broken_images now has an array of any images on the page with broken links
    # and we want to ensure that it doesn't have any items
    assert broken_images.empty?
    

    To your other question, I would recommend just taking a screenshot of the page and having a human manually verify the resulting screenshot has the correct images. Computers can do the automation work, but humans do have to check and verify its results from time to time :)

提交回复
热议问题