如何使用 Selenium WebDriver 检查 404 的图像请求?

如何使用 Selenium WebDriver 检查 404 的图像请求?

问题描述:

使用 Selenium WebDriver 检查 URL GET 是否成功返回 (HTTP 200) 的最便捷方法是什么?

What is the most convenient way using Selenium WebDriver to check if an URL GET returns successfully (HTTP 200)?

在这种特殊情况下,我最感兴趣的是验证当前页面的图像没有损坏.

In this particular case I'm most interested in verifying that no images of the current page are broken.

试试这个:

List<WebElement> allImages = driver.findElements(By.tagName("img"));
for (WebElement image : allImages) {
  boolean loaded = ((JavaScriptExecutor) driver).executeScript(
      "return arguments[0].complete", image);
  if (!loaded) {
    // Your error handling here.
  }
}