Search code examples
htmlxmlxpath

Xpath that find a specific text and only this specific text


I'm using an xpath to locate element that contains a certain text in it. My problem is that it locates another element that has the same text I'm looking for in it but also some other text, here is the xpath I'm using:

//a[contains(text(), 'Workflow')]

And I want to locate a link that contain the text Workflow and Workflow only, but the xpath locates a link with Workflow.MAINMENU which I don't want to.

Is this possible with an XPATH?


Solution

  • Yes, this is possible. You need to not use the contains function, but to instead compare the text directly:

    //a[text() = 'Workflow']
    

    If there is whitespace surrounding the text, you could use:

    //a[normalize-space(text()) = 'Workflow']