Selenium:我如何获得图像的src? [英] Selenium: How do I get the src of an image?

查看:4302
本文介绍了Selenium:我如何获得图像的src?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Selenium (within PHPUnit) to test my web application. I would like to test whether a certain image which is present on the page really exists. More precisely, whether the image url returns 404 or not. In order to test that, I need to get the image url. Given the image locator, How do I get the value of its src attribute (its url)?

推荐答案

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

Assuming that you have an image in a WebElement (lets say img), in Java world you can retrieve the link below

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

Editing the answer to clarify. By Java world I mean Selenium 2.0 Java bindings. In Selenium 2.0 (of course if you are using webdriver) has a class called WebElement representing elements on the page. getAttribute is a selenium Method in Java binding.

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");

也许在PHPUnit中有类似的东西

Perhaps there is something similar in PHPUnit

这篇关于Selenium:我如何获得图像的src?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆