首页 > 代码库 > java selenium爬取验证图片是否加载完成
java selenium爬取验证图片是否加载完成
爬虫任务里发现有部分图片没有加载完成就进行文件流上传,导致有一些图片是空白,需要判断一下:
首选获取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 判断该图片是否存在
java selenium爬取验证图片是否加载完成
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。