Given ab...">

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!

+75
xpath
Jan 05 '10 at 21:35
source share
4 answers
 //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.

+123
Jan 05 '10 at 21:37
source share

or //div[@id='id-74385'][@class='guest clearfix']

+64
Jan 05 '10 at 21:45
source share

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')] .

+6
Mar 24 '17 at 10:04 on
source share

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

+1
Sep 05 '13 at 5:44 on
source share



All Articles