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

Image Selenium:如何获得图像的src?,image,selenium,Image,Selenium,我正在使用Selenium(在PHPUnit中)测试我的web应用程序。我想测试页面上的某个图像是否真的存在。更准确地说,无论图像url是否返回404。为了测试这一点,我需要获取图像url。给定图像定位器,如何获取其src属性(其url)的值?例如,使用xPath //img[@src='The image src'] 但不幸的是,我不确定这是否是测试图像是否已加载的好方法假设您在WebElement(比方说img)中有一个图像,在Java世界中,您可以检索下面的链接 编辑答案以澄清。 Jav

我正在使用Selenium(在PHPUnit中)测试我的web应用程序。我想测试页面上的某个图像是否真的存在。更准确地说,无论图像url是否返回404。为了测试这一点,我需要获取图像url。给定图像定位器,如何获取其src属性(其url)的值?例如,使用xPath

//img[@src='The image src']

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

假设您在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");

也许PHPUnit中也有类似的东西,您可以将“getAttribute”与XPath一起使用来获取图像src(或任何其他属性:alt、title、width等)


如果您使用的是任何类似PHP的webdriver,那么它将非常容易地解决您的问题

例如:

1) 创建wedriver对象:

public function setUp()
{
parent::setUp();
$web_driver = new WebDriver();
$element = $web_driver->session();
}
使用此
$element
变量,您可以通过以下行轻松访问任何HTML属性:

$element->element('xpath', '//*[@id="logo"]')->attribute('src');

这将返回
src
attibute的值。您可以使用此行获取任何属性,如class、id、title、alt等。

您需要使用XPath。对于PHPUnit和Selenium,获取图像的src属性的语法为:

$imageUrl = $this->getAttribute("//img/@src");
你必须使用

storeAttribute

在目标地点,你必须这样说

//img[@src='imagename.png']@src

并将其存储在一个变量中

img

现在使用

验证属性

例如:命令:

验证属性

目标:

//img[@src='imagename.png']@src

价值:

${img}


下面的代码将帮助您获得所有图像及其URL的列表。 您也可以在img上使用script来获取javascript文件列表

package seleniumlinkpackage; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class xmenfirstclass { public static void main(String[] args) throws InterruptedException { String url = "Paste Your URL here"; WebDriver driver = new FirefoxDriver(); driver.get(url); List links=driver.findElements(By.tagName("img")); // this will display list of all images exist on page for(WebElement ele:links){ System.out.println(ele.getAttribute("src")); } //Wait for 5 Sec Thread.sleep(5); // Close the driver driver.quit(); } } 包装硒链接包装; 导入java.util.List; 导入org.openqa.selenium.By; 导入org.openqa.selenium.WebDriver; 导入org.openqa.selenium.WebElement; 导入org.openqa.selenium.firefox.FirefoxDriver; 公共类xmenfirstclass{ 公共静态void main(字符串[]args)引发InterruptedException{ String url=“在此处粘贴您的url”; WebDriver=newfirefoxdriver(); 获取(url); 列表链接=driver.findElements(按.tagName(“img”)); //这将显示页面上存在的所有图像的列表 for(WebElement元素:链接){ System.out.println(ele.getAttribute(“src”); } //等5秒钟 睡眠(5); //关闭驱动器 driver.quit(); } }
在chrome中,可以用java编写代码来获取图像src列表

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;


public class imageUrl {
    public static void main(String[] args) {

                //Create Driver-object for chrome browser
                System.setProperty("webdriver.chrome.driver","//Users//admin//Desktop//chromedriver");
                WebDriver driver = new ChromeDriver();

                //get the page
                driver.get("https://jet.com/");

                // this will find the elements by tag name. Don't forget to write "WebElement" after List.
                //or else it will give an error on the for loop

                List<WebElement>links=driver.findElements(By.tagName("img"));

                // this will display list of all images exist on page
                for(WebElement ele:links){
                    System.out.println(ele.getAttribute("src"));
                }
    }

}
import java.util.List;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
公共类imageUrl{
公共静态void main(字符串[]args){
//为chrome浏览器创建驱动程序对象
System.setProperty(“webdriver.chrome.driver”,“//Users//admin//Desktop//chromedriver”);
WebDriver驱动程序=新的ChromeDriver();
//获取页面
驱动程序。获取(“https://jet.com/");
//这将按标记名查找元素。别忘了在列表后写上“WebElement”。
//否则它将在for循环中给出一个错误
Listlinks=driver.findElements(按.tagName(“img”));
//这将显示页面上存在的所有图像的列表
for(WebElement元素:链接){
System.out.println(ele.getAttribute(“src”);
}
}
}

driver.findElement(By.cssSelector(“img”)).getAttribute(“src”)

我没有问“如何通过src检索图像”。我问过“给定图像,如何检索其src”。我已经有了图像,但我不知道它的src。它的src是我需要得到的,我不在Java世界,而是在Selenium世界。在Selenium世界中,我没有表示元素的对象。因此,我不能那么容易地获得img属性。所谓Java世界,我指的是Java中的Selenium 2.0绑定。查看我编辑的答案。假设这是OP接受的答案。。。?到目前为止已经8年了。
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;


public class imageUrl {
    public static void main(String[] args) {

                //Create Driver-object for chrome browser
                System.setProperty("webdriver.chrome.driver","//Users//admin//Desktop//chromedriver");
                WebDriver driver = new ChromeDriver();

                //get the page
                driver.get("https://jet.com/");

                // this will find the elements by tag name. Don't forget to write "WebElement" after List.
                //or else it will give an error on the for loop

                List<WebElement>links=driver.findElements(By.tagName("img"));

                // this will display list of all images exist on page
                for(WebElement ele:links){
                    System.out.println(ele.getAttribute("src"));
                }
    }

}