Selenium:我如何获得图像的src?

sna*_*ile 18 selenium image

我正在使用Selenium(在PHPUnit中)来测试我的Web应用程序.我想测试页面上存在的某个图像是否确实存在.更确切地说,图像URL是否返回404.为了测试它,我需要获取图像网址.给定图像定位器,如何获取其src属性(其url)的值?

nil*_*esh 28

假设您在WebElement中有一个图像(比如img),在Java世界中您可以检索下面的链接

编辑答案以澄清. 在Java世界中,我指的是Selenium 2.0 Java绑定.在Selenium 2.0中(当然,如果您使用的是webdriver)有一个名为WebElement的类,表示页面上的元素.getAttribute是Java绑定中的selenium方法.

String url = "http://www.my.website.com";
WebDriver driver = new FirefoxDriver();
driver.get(url);
WebElement img = driver.findElement(By.id("foo"));
String src = img.getAttribute("src");
Run Code Online (Sandbox Code Playgroud)

也许PHPUnit中有类似的东西


小智 -3

例如使用 xPath

//img[@src='The image src']
Run Code Online (Sandbox Code Playgroud)

但不幸的是我不确定这是测试图像是否已加载的好方法