使用Selenium Webdriver检查网站上的图像时发生异常

使用Selenium Webdriver检查网站上的图像时发生异常,selenium,selenium-webdriver,webdriver,Selenium,Selenium Webdriver,Webdriver,我是硒的新手。我试图检查是否标题的标志,如果一个网站显示或没有。我已经写了关于如何检查图像是否显示在互联网上搜索后的代码。如果我使用函数isDisplayed(),代码工作正常,但是我使用JavascriptExecutor尝试了它,我得到了一个我不理解的异常 代码如下: import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDrive

我是硒的新手。我试图检查是否标题的标志,如果一个网站显示或没有。我已经写了关于如何检查图像是否显示在互联网上搜索后的代码。如果我使用函数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)
线程“main”org.openqa.selenium.WebDriverException中的异常:语法错误 命令持续时间或超时:38毫秒 构建信息:版本:“2.39.0”,修订版:“ff23eac”,时间:“2013-12-16 16:12:12” 系统信息:主机:'Zac PC',ip:'192.168.73.1',os.name:'Windows 7',os.arch:'amd64',os.version:'6.1',java.version:'1.7.0_55' 会话ID:b727b844-9798-4d26-9362-23273d87abc9 驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver 能力[{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}] 位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 位于sun.reflect.NativeConstructorAccessorImpl.newInstance(未知源) 位于sun.reflect.delegatingConstructor或AccessorImpl.newInstance(未知源) 位于java.lang.reflect.Constructor.newInstance(未知源) 位于org.openqa.selenium.remote.ErrorHandler.CreateTrowable(ErrorHandler.java:193) 位于org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145) 位于org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554) 位于org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:463) 在Link_test.main(Link_test.java:39) 原因:org.openqa.selenium.remote.ErrorHandler$UnknownServerException:语法错误 构建信息:版本:“2.39.0”,修订版:“ff23eac”,时间:“2013-12-16 16:12:12” 系统信息:主机:'Zac PC',ip:'192.168.73.1',os.name:'Windows 7',os.arch:'amd64',os.version:'6.1',java.version:'1.7.0_55' 驱动程序信息:驱动程序。版本:未知 在handleEvaluateEvent(http://northeastindiaholidays.com/:67)
页面链接可以正常打开,但检查似乎不起作用。这是为什么?

请尝试下面的代码。在查找元素时,请始终使用隐式等待。对于图像,建议在可用时使用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();
}}

类似地,对其他图像执行此操作

非常感谢Bharath。但我有两个问题-1.如果图像没有alt标记,我该怎么办?2.为什么建议对图像使用CSS选择器,而不是xpath?1.可以使用其他属性,如'src'值,或者如果没有其他选项,则可以使用xpath。2.Css描述元素的外观和格式(最适合您案例中的图像),通过Css选择器识别元素也比xpath快。您也可以使用xpath,但可以使用相对xpath(您使用的是绝对xpath)。您案例中的相对xpath示例为:By.xpath(//h1[@class='logo']/a/img)再次感谢巴拉斯。