0

Hello I have a parent element with the ID contacts that element has a child element that contains the text "hello" I would like to find that child element as a child of the element having the ID contacts. This is because "hello" occur on several places on the page but I only the want the "hello" that has contacts as parent.

Example of what I mean:

"//*[@id='contacts']/[contains(text(),'hello')]"

This does not work sadly

petro99
  • 35
  • 6

2 Answers2

1

This should work! the easiest way to try this live, is in Chromes dev tools.

//*[@id='latest_block']//*[text()[contains(.,'hello')]]

Find out more how Xpath works here:

XPath contains(text(),'some string') doesn't work when used with node with more than one Text subnode

E P
  • 389
  • 4
  • 16
0

You are missing a tag name for the child element, so just change your XPath to

"//*[@id='contacts']/*[contains(text(),'hello')]"

In case the child element is not a direct child of //*[@id='contacts'] element your XPath should be

"//*[@id='contacts']//*[contains(text(),'hello')]"
Prophet
  • 32,350
  • 22
  • 54
  • 79