Search code examples
javascripttestingxpathweb-deployment

Xpath find an element containing text within the scope of a parent element


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


Solution

  • 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