等待图像完全加载Selenium WebDriver

编程入门 行业动态 更新时间:2024-10-28 12:18:57
本文介绍了等待图像完全加载Selenium WebDriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我看到有一个问题: Selenium-Webdriver Ruby->单击后如何等待图像完全加载

但是似乎没有人直接回答:我需要等待使用Selenium WebDriver将图像完全加载,而不仅仅是在DOM中显示.我该怎么办?目前我正在使用

But it seems that nobody has directly answered that: I need to wait for an image to be fully loaded, not just to present in the DOM, using selenium WebDriver. What should I do?Currently I'm using

public static void waitUntilVisible(WebDriver webDriver, By locator) { WebDriverWait driverWait = new WebDriverWait(webDriver, TIMEOUT); driverWait.until(visibilityOfElementLocated(locator)); }

使用

/** * An expectation for checking that an element is present on the DOM of a page and visible. * Visibility means that the element is not only displayed but also has a height and width that is * greater than 0. * * @param locator used to find the element * @return the WebElement once it is located and visible */ public static ExpectedCondition<WebElement> visibilityOfElementLocated( final By locator) { return new ExpectedCondition<WebElement>() { @Override public WebElement apply(WebDriver driver) { try { return elementIfVisible(findElement(locator, driver)); } catch (StaleElementReferenceException e) { return null; } } @Override public String toString() { return "visibility of element located by " + locator; } }; }

但是,如果图像已经具有高度和宽度,即使未加载图像,它也可能被视为可见

but if an image has already an height and width it's probably counted as visible even if the image hasn't loaded

推荐答案

您可以执行一些JavaScript来查询DOM.具体来说,是 HTMLImageElement 的complete属性.. >

You could execute some javascript to interrogate the DOM. Specifically, the complete property of an HTMLImageElement.

public static void waitUntilVisible(WebDriver webDriver, By locator) { WebDriverWait driverWait = new WebDriverWait(webDriver, TIMEOUT); WebElement el = webDriver.findElement(locator); driverWait.until( new Predicate<WebDriver>() { public boolean apply(WebDriver driver) { return ((JavascriptExecutor)driver).executeScript("return arguments[0]plete", el); } } ); }

但是请注意,complete:

如果浏览器已完成获取操作,则返回一个布尔值,该布尔值为true 图像,无论是否成功.如果图像还显示为真 没有src值.

Returns a Boolean that is true if the browser has finished fetching the image, whether successful or not. It also shows true, if the image has no src value.

更多推荐

等待图像完全加载Selenium WebDriver

本文发布于:2023-11-08 10:42:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569168.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   加载   WebDriver   Selenium

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!