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)?
11 Answers
Assuming that you have an image in a WebElement (lets say img), in Java world you can retrieve the link below
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");
Perhaps there is something similar in PHPUnit

- 14,131
- 7
- 65
- 79
-
I'm not in Java world, but in Selenium world. In Selenium world I don't have an object which represents the element. So I cannot get the img attributes that easily. – snakile Aug 31 '11 at 07:33
-
By Java world I meant Selenium 2.0 bindings in Java. See my edited answer. – nilesh Aug 31 '11 at 15:31
-
Assuming this is the OP's accepted answer...? It's been 8 years so far. – Brett Rigby Feb 01 '19 at 08:10
Below code will help you to get the list of all images and their URLs. You can also use script instead on img to get the list of javascript files.
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(); } }

- 101
- 2
You can use 'getAttribute' with XPath to get an image src (or any other attribute: alt, title, width, etc.)
selenium.getAttribute("//img/@src");

- 5,015
- 3
- 35
- 28
If You are you using any PHP webdriver like facebook/phpwebdiver, then it will solve your problem very easily.
Example :
1) Create a wedriver object:
public function setUp()
{
parent::setUp();
$web_driver = new WebDriver();
$element = $web_driver->session();
}
With this $element
variable you can easily access any HTML attribute with the following line:
$element->element('xpath', '//*[@id="logo"]')->attribute('src');
This will return you the value of src
attibute. You can use this line to get any attribute like class, id, title, alt, etc...

- 39,123
- 5
- 88
- 127

- 101
- 6
You need to use XPath. For PHPUnit and Selenium the Syntax for getting the src attribute of an image is:
$imageUrl = $this->getAttribute("//img/@src");

- 10,288
- 6
- 35
- 32
you have to use
storeAttribute
And at the target place u have to mention like this
//img[@src='imagename.png']@src.
And store that in a variable lets say
img
Now verify that by using
verifyAttribute
eg:command:
verifyAttribute
target:
//img[@src='imagename.png']@src
value:
${img}

- 206
- 2
- 8
Well, in chrome one can write code in java to get the list of image 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"));
}
}
}

- 21
- 1
- 7
To print the value of the src
attribute of the <img>
element you need to locate the element first and then use getAttribute()
method to retrieve the attribute value as follows:
String src = driver.findElement(By.id("imageID")).getAttribute("src");
To print the value within the console:
System.out.println(driver.findElement(By.id("imageID")).getAttribute("src"));
Incase the <img>
element is dynamically loaded you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use the following locator strategies:
System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.id("imageID"))).getAttribute("src"));

- 183,867
- 41
- 278
- 352
use xPath, for example
//img[@src='The image src']
but unfortunately i am not sure that it is a good way to test if image was loaded

- 379
- 2
- 11
-
1I didn't ask "how to retrieve an image by its src". I've asked "given the image, how to retrieve its src". I already have the image, but I don't know its src. Its src is what I need to get. – snakile Aug 30 '11 at 14:50