How to write an XPath query to match two attributes?
Next question:
<div id="id-74385" class="guest clearfix" style="z-index: 999;"> Given above
If I want an XPath expression with checks for both id and class, can we do this w / 'and' condition LIKE:
//div[@id='id-74385'] and div[@class='guest clearfix'] Is it correct? My execution fails here ... Please help!
//div[@id='..' and @class='...] gotta do the trick. This is a selection of div statements that have both attributes of the desired value.
It is worth using one of the XPath online tests to try something like that.
or //div[@id='id-74385'][@class='guest clearfix']
Addendum to Brian Agnew's answer.
You can also make //div[@id='..' or @class='...] and you can bracket the expressions inside //div[@id='..' and (@class='a' or @class='b')] .
XML example:
<X> <Y ATTRIB1=attrib1_value ATTRIB2=attrib2_value/> </X> string xPath="/" + X + "/" + Y + "[@" + ATTRIB1 + "='" + attrib1_value + "']" + "[@" + ATTRIB2 + "='" + attrib2_value + "']" XPath Testbed: http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm