使用Selenium Webdriver检查网站上的图像时出现异常

时间:2014-06-02 18:08:53

标签: selenium selenium-webdriver webdriver

我是Selenium的新手。我试图检查是否显示网站的标题徽标。我在搜索互联网后编写了关于如何检查图像是否显示的代码。如果我使用函数isDisplayed(),代码工作正常,但我尝试使用JavascriptExecutor,我得到一个我不明白的异常。

以下是代码:

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Header_logo {

public WebDriver driver;
private String baseUrl;

public static void main(String[] args) {

    Header_logo obj1=new Header_logo();
    obj1.launch_test();
    // TODO Auto-generated method stub

}

public void launch_test() 
{
    driver = new FirefoxDriver();
    baseUrl = "http://northeastindiaholidays.com";
    driver.get(baseUrl);

    WebElement image1 = driver.findElement(By.xpath("html/body/header/div/h1/a/img"));

    Boolean imageLoaded1 = (Boolean) ((JavascriptExecutor)driver).executeScript("return arguments[0].complete && typeof arguments[0].naturalWidth != \"undefined\" && arguments[0].naturalWidth > 0", image1);
    if (!imageLoaded1)
    {
         System.out.println("1. Image is not present");
    }
    else
    {
        System.out.println("1. Got it");
    }

    WebElement image2 = driver.findElement(By.xpath("html/body/header/div/div[3]"));

    Boolean imageLoaded2 = (Boolean) ((JavascriptExecutor)driver).executeScript("return arguments[0].complete && typeof arguments[0].naturalWidth != \"undefined\" && arguments[0].naturalWidth > 0", image2);
    if (!imageLoaded2)
    {
         System.out.println("2. Image is not present");
    }
    else
    {
        System.out.println("2. Got it");
    }

    driver.close();
}

}

每次运行此代码时都会收到此异常

Exception in thread "main" org.openqa.selenium.WebDriverException: syntax error
Command duration or timeout: 38 milliseconds
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12'
System info: host: 'Zac-PC', ip: '192.168.73.1', os.name: 'Windows 7', os.arch: 'amd64',      os.version: '6.1', java.version: '1.7.0_55'
Session ID: b727b844-9798-4d26-9362-23273d87abc9
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true,   cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=29.0.1}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:463)
at Link_test.main(Link_test.java:39)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: syntax error
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12'
System info: host: 'Zac-PC', ip: '192.168.73.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_55'
Driver info: driver.version: unknown
at <anonymous class>.handleEvaluateEvent(http://northeastindiaholidays.com/:67)

页面链接打开正常,但检查似乎不起作用。那是为什么?

1 个答案:

答案 0 :(得分:2)

请尝试以下代码。在查找元素时也始终使用隐式等待。对于图像,建议在可用时使用 cssSelector

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Header_logo {

public WebDriver driver;
private String baseUrl;

public static void main(String[] args) {

TestDriver obj1=new TestDriver();
obj1.launch_test();
// TODO Auto-generated method stub

}

public void launch_test() 
{
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
baseUrl = "http://northeastindiaholidays.com";
driver.get(baseUrl);

WebElement image = driver.findElement(By.cssSelector("img[alt='North East India Holidays']"));
//use this if you have alt attribute in that image element 
String imgName = driver.findElement(By.cssSelector("img[alt='North East India Holidays']")).getAttribute("alt");

Boolean loaded = (Boolean) ((JavascriptExecutor)driver).executeScript("return arguments[0].complete && " + "typeof arguments[0].naturalWidth != \"undefined\" && " + "arguments[0].naturalWidth > 0", image);
if (loaded){
     System.out.println(imgName + " Image is present and status is : " + loaded);
}
else
{
    System.out.println(imgName + "Image is NOT present and status is : " + loaded);
}

driver.close();
}}

类似地为其他图像做