2017-10-17 36 views
0

我想驗證使用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 
+0

我不知道硒,所以這可能是完全無用的 - 但有沒有機會在頁面加載之前進行評估?有沒有辦法可以附加到圖像元素的'.load'事件,並在發生火災時進行評估? – theGleep

回答

0

我覺得你想在這裏太花哨。在這種情況下,你並不需要JSE。您只需使用.getAtribute("naturalWidth")並比較預期回報。

WebElement Imglogo = driver.findElement(By.xpath("//img[@src='Images/Logo.png']")); 
System.out.println(Imglogo.getAttribute("naturalWidth")); 
System.out.println(Imglogo.getAttribute("naturalWidth").equals("0")); 

...等等。

+0

我試過了你的建議。但它仍然返回false。 – sjkp

+0

你試過的具體代碼是否返回false? – JeffC

相關問題