如何在 selenium java 中等待,直到网页上的所有图像都正确加载

当所有图像都完全加载到其中时,我正在尝试截取网页的屏幕截图。


虽然我认为解决方案已经在堆栈溢出时可用,但它们都不适合我。我找到的最接近的解决方案是:等待图像完全加载 Selenium WebDriver


然而,在这段代码中,用 javascript 执行器编写的 arg 没有返回布尔输出,这就是它给我一个错误的原因。


以下我正在使用的代码,但在我编译时出现错误,因为使用 equals 的“不兼容的操作数类型 Object 和 int”在这里也不适用于我


 waitexternal.until(new Function<WebDriver, Boolean>(){

    @Override

    public Boolean apply(WebDriver ldriver) {

        ldriver = driver;

        JavascriptExecutor js = (JavascriptExecutor) driver;  


        return js.executeScript("return jQuery.active") == 0;


        //return (js.executeScript("return arguments[0]").equals("complete"));

        }

    }


   );


白衣染霜花
浏览 235回答 2
2回答

翻阅古今

您可以使用 识别当前页面中的所有图像document.images。您可以使用 . 检查当前页面中有多少图像document.images.length。要检查这些图像是否正确加载,您可以complete像这样在每个图像上应用属性,document.images[0].complete如果图像已加载,则返回 true,否则返回 false。下面是DOM首先检查是否加载的代码,然后如果加载了 DOM,它将等到所有图像都加载到该页面中。// Launching the browserdriver.get("http://www.google.com");// Declaring and Casting driver to JavaScriptExecutorJavascriptExecutor jse = (JavascriptExecutor) driver;// Getting DOM statusObject result = jse.executeScript("return document.readyState;");System.out.println("=> The status is : "+result.toString());// Checking DOM loading is completed or not?if(result.equals("complete")) {&nbsp; &nbsp; // Fetching images count&nbsp; &nbsp; result = jse.executeScript("return document.images.length");&nbsp; &nbsp; int imagesCount = Integer.parseInt(result.toString());&nbsp; &nbsp; boolean allLoaded = false;&nbsp; &nbsp; // Checking and waiting until all the images are getting loaded&nbsp; &nbsp; while(!allLoaded) {&nbsp; &nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0;i<imagesCount;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = jse.executeScript("return document.images["+i+"].complete;");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean loaded = (Boolean) result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(loaded) count++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Breaking the while loop if all the images loading completes&nbsp; &nbsp; &nbsp; &nbsp; if(count == imagesCount) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("=> All the Images are loaded...");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("=> Not yet loaded...");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(1000);&nbsp; &nbsp; }}但是在使用上面的代码时要小心,因为如果元素根本没有加载,它有时可能会进入无穷大状态。要检查特定元素是否已加载,您可以执行以下操作:// To check a particular element is loaded or not?WebElement googleLogo = driver.findElement(By.id("hplogo"));boolean loaded = (Boolean) jse.executeScript("return arguments[0].complete;", googleLogo);System.out.println("The google logo is loaded ? "+loaded);要等到加载该特定图像,您可以执行以下操作:// To check a particular element is loaded or not?WebElement googleLogo = driver.findElement(By.id("hplogo"));while(!(Boolean) jse.executeScript("return arguments[0].complete;", googleLogo)) {&nbsp; &nbsp; System.out.println("=> The google logo is not yet loaded...");&nbsp; &nbsp; Thread.sleep(1000);}System.out.println("The google logo is loaded... ");要了解有关 JavaScript 命令和 JavaScriptExecutor 的更多信息,请查看并订阅此频道

ABOUTYOU

你可以试试下面的代码。我正在使用它,它对我有用。public Boolean verifyImageLoad() throws Exception {&nbsp; &nbsp; &nbsp;wait.until(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new Function<WebDriver, Boolean>(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public Boolean apply(WebDriver driver) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (Boolean) ((JavascriptExecutor)driver).executeScript("return arguments[0].complete", imageWebElementHere);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; if (!imageWebElementHere.getAttribute("naturalWidth").equals("0")){&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; return false;}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java