XPath to Parse "SRC" from IMG tag?

dMix picture dMix · Jul 24, 2009 · Viewed 56.8k times · Source

Right now I successfully grabbed the full element from an HTML page with this:

//img[@class='photo-large']

for example it would return this:

<img src="http://example.com/img.jpg" class='photo-large' />

But I only need the SRC url (http://example.com/img.jpg). Any help?

Answer

Jeff Yates picture Jeff Yates · Jul 24, 2009

You are so close to answering this yourself that I am somewhat reluctant to answer it for you. However, the following XPath should provide what you want (provided the source is XHTML, of course).

//img[@class='photo-large']/@src

For further tips, check out W3 Schools. They have excellent tutorials on such things and a great reference too.