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 these with no luck:
//div[@class="post-content"]//img[1] and
//div[@class="post-content"]//img[position()=1]
XPath index is defined as per specific nodes within the node-set for index by using selenium webdriver. We can also mention the index-specific node with the help of a number of indexes enclosed by []. The tag name element UI contains multiple child elements with tag name as li.
Such usage is almost always a bug because in XPath, the index starts at 1 , not at 0 .
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]
indexes in XPath are 1-based, not 0-based. Try
(//div[@class="post-content"]//img)[position()=2]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With