mloureiro
mloureiro

Reputation: 949

Through XPath find an element that has a child with specific text

I've seen similar questions on the wild, but for some reason I couldn't manage yet to find a correct way to do.

So I got this page

Under main items there are 2 articles (shop items)

  1. eZ Publish - Man jacket
  2. eZ Publish - iPhone 4 Case

I want to get the input (text) field in the article that contains the man jacket

What have I already tried:

//article[ //h3[ text() = "eZ Publish - Man jacket"] ]//input
//article[ //h3[contains(text(), "eZ Publish - Man jacket")] ]//input
//article[ contains( .//h3[ text() = 'eZ Publish - Man jacket' ] ) ]//input

The next one works:

//h3[text()='eZ Publish - Man jacket']/../../..//input

... but this isn't kind of an option...

Any idea what am I doing wrong?

Upvotes: 0

Views: 2278

Answers (2)

paul trmbrth
paul trmbrth

Reputation: 20748

You probably meant //article[.//h3[ text() = "eZ Publish - Man jacket"] ]//input, that is with a relative XPath expression (starting with '.') in the article context

Upvotes: 1

Neil
Neil

Reputation: 413

Your first XPath expression works, however according to your source XML you won't see any values as your "input" elements don't have values, rather they have attributes that have values.

If you want to output all the attribute values you would need to add /@* after "input" to specify all (*) the attributes (/@).

Your second XPath expression won't work as you are trying to pass a node-tree (text() which is being run against //h3 therefore returning multiple nodes) as a string argument to the contains function.

Your third XPath expression won't work as you are only passing one argument to contains, which requires two.

Hope this helps.

Upvotes: 0

Related Questions