yak_ilnur
yak_ilnur

Reputation: 95

How to find element by attribute value?

I want to find the <issue> where id = 6. How do I do it? I tried the following, but it didn't work:

<issues>
<issue>
  <id>7</id>
  <project id="1" name="testProject"/>      
  <author id="1" name="testAuthor"/>     
  <subject>subject</subject>
  <start_date>2015-08-24</start_date>      
</issue>
<issue>
  <id>6</id>
  <project id="2" name="testProject2"/>      
  <author id="2" name="testAuthor2"/>     
  <subject>subject2</subject>
  <start_date>2015-08-24</start_date>    
</issue>
</issues>

My XPath expression attempts are:

doc.xpath("//id[contains(text(), '6']") 

and

doc.xpath("//issue[@id=6]") 

Upvotes: 1

Views: 1327

Answers (1)

har07
har07

Reputation: 89325

You can simply use the following XPath expression to get the issue element having a child element where id equals 6:

//issue[id=6]

id is a child element, not an attribute so you don't use @id for this task.

Upvotes: 2

Related Questions