我想驗證使用Selenium在網頁中加載的圖像。以下是我用於相同的路線。 'ImagePresent'總是返回'false',即使它能夠從最後一行捕獲並打印圖像的屬性值。使用JavascriptExecutor進行圖像驗證,即使在加載圖像時也總是返回'false'
WebElement Imglogo = driver.findElement(By
.xpath("//img[@src='Images/Logo.png']"));
Boolean ImagePresent = (Boolean) ((JavascriptExecutor) driver)
.executeScript(
"return arguments[0].complete && typeof arguments[0].naturalWidth != \"undefined\" && arguments[0].naturalWidth > 0",
Imglogo);
System.out.println("The boolean value of ImagePresent: "+ImagePresent); //returns false
System.out.println("Image Attribute: "+Imglogo.getAttribute("src")); //prints the full path
我不知道硒,所以這可能是完全無用的 - 但有沒有機會在頁面加載之前進行評估?有沒有辦法可以附加到圖像元素的'.load'事件,並在發生火災時進行評估? – theGleep