xpath get element by index

I have the below xpath expression

//div[@class="post-content"]//img

which runs on a html page, scanning for images. The above query returns a lot of images but I only want the second in the list.

I have tried

//div[@class="post-content"]//img[1] and
//div[@class="post-content"]//img[position()=1]

with no luck

How can I do it?

thanks

Answers


In XPath index starts from 1 position, therefore

//div[@class="post-content"]//img[2]

should work correctly if you have to select each 2nd img in div[@class="post-content"]. If you have to select only 2nd img from all images that are in div[@class="post-content"], use:

(//div[@class="post-content"]//img)[2]

Need Your Help

why does this reader read off strange bits of data?

java bufferedreader fileinputstream

I'm trying to read a text file, i'm using fileImputStream, and reading all the lines into a single String then outputing it into the console (System.out)

Add html element inside of form element using cake php form helper

php html cakephp formhelper

I am trying to create a simple html output that looks like this

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.