Sam
Sam

Reputation: 5647

What is the proper way to use descendant in XPath

I am trying to find all DIV elements have the attribute widget-name and a descendant span tag that have a title attribute.

This is what I am trying.

//div[@widget-name and descendant::span[@title]]"

This seems to almost work but it is missing one element in the Nodes Collection it returns.

Upvotes: 2

Views: 914

Answers (2)

Daniel Haley
Daniel Haley

Reputation: 52858

This XPath should return all div's that:

  • has a widget-name attribute
  • has a descendant span element (used abbreviated syntax) that:
    • has a class attribute with the value 'title'
    • contains the text 'someTextToKeep' (if you want to exclude spans with certain text, wrap the contains() in not().

XPath:

//div[@widget-name and .//span[@class='title'][contains(.,'someTextToKeep')]]

Upvotes: 0

Sam
Sam

Reputation: 5647

Never mind.

This is what I needed:

//div[@widget-name and descendant::span[@class='title']]

OK - take it back. This is not the complete answer. I am now trying to tweak this to where it returns all except where title is not equal to some text:

//div[@widget-name and descendant::span[@class='title' and [text()[contains(., '{someTextToKeep}'

Anyone see why this would be invalid XPath?

Final answer is:

//div[@widget-name and descendant::span[@class='title' and text()[not(contains(., 'someTextToKeep'))]]]"

Upvotes: 1

Related Questions