TabooSheen
TabooSheen

Reputation: 13

Xpath - How to select an element that contains a text but nothing more

I am trying to select an element that contains a piece of text but I do not want to select the elements that contain the text plus additional text. I would normally use text()='abc def', but this particular element contains spaces before and after.

Here is an example snippet:

<div> 
     <div>
          abc def
     </div>
     <div>
          abc def ghi
     </div>
     <div>
          abc def ghi jkl
     </div>
</div>

I want to select the first div but //div[text()='abc def'] does not work because of the leading spaces and //div[contains(text(),'abc def'] also does not work because it will select all of the divs.

I have looked into normalize-space and starts-with, but I could not get it to quite work.

Upvotes: 1

Views: 328

Answers (2)

OldProgrammer
OldProgrammer

Reputation: 12159

What about this?

//div[normalize-space()='abc def']/text()

Upvotes: 2

eLRuLL
eLRuLL

Reputation: 18799

'//div/div[normalize-space(text())="abc def"]'

Upvotes: 0

Related Questions