爬虫任务里发现有部分图片没有加载完成就进行文件流上传,导致有一些图片是空白,需要判断一下:

首选获取image标签元素:

WebElement image = driver.findElement(By.cssSelector("div#media_container > img"));
boolean isVisible = this.IsImageVisible(driver, image);

然后判断:

private boolean IsImageVisible(WebDriver driver,WebElement image){
Boolean imageLoaded1 = (Boolean) ((JavascriptExecutor)driver).executeScript("return arguments[0].complete && typeof arguments[0].naturalWidth != \"undefined\" && arguments[0].naturalWidth > 0", image);
if (!imageLoaded1)
{
return false;
}
return true;
}

complete 属性判断有没有加载完成,naturalWidth 判断该图片是否存在

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-03-04
  • 2022-02-08
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案