您可以使用
document.images
. 您可以使用
document.images.length
.
要检查这些图像是否正确加载,可以应用
complete
像这样的每个图像的属性
document.images[0].complete
如果图像加载为false,则返回true。
下面是检查
DOM
是否先加载,然后如果加载了dom,它将等待所有图像加载到该页中。
// Launching the browser
driver.get("http://www.google.com");
// Declaring and Casting driver to JavaScriptExecutor
JavascriptExecutor jse = (JavascriptExecutor) driver;
// Getting DOM status
Object 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")) {
// Fetching images count
result = jse.executeScript("return document.images.length");
int imagesCount = Integer.parseInt(result.toString());
boolean allLoaded = false;
// Checking and waiting until all the images are getting loaded
while(!allLoaded) {
int count = 0;
for(int i=0;i
result = jse.executeScript("return document.images["+i+"].complete;");
boolean loaded = (Boolean) result;
if(loaded) count++;
}
// Breaking the while loop if all the images loading completes
if(count == imagesCount) {
System.out.println("=> All the Images are loaded...");
break;
} else {
System.out.println("=> Not yet loaded...");
}
Thread.sleep(1000);
}
}
但是在使用上面的代码时要小心,因为如果根本不加载元素,它有时可能会进入无限状态。
要检查是否加载了特定元素,可以执行以下操作:
// 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)) {
System.out.println("=> The google logo is not yet loaded...");
Thread.sleep(1000);
}
System.out.println("The google logo is loaded... ");
要了解更多关于javascript命令和javascriptexecutor的信息,请签出并订阅
this channel
我希望它能帮助…