you can also do it this way
el = doc.xpath("id(spam[500])").first
for elements having id's you can call them directly through id because its always unique.
for finding out xpaths, please add a 'xpath checker' plugin to firefox. It helps a lot and very easy