Reputation: 331
I am trying to use XPath function contains()
that has a string in 2 parts but it is throwing an "invalid xpath expression" error upon evaluation.
Here is what I am trying to achieve:
Normal working xpath:
//*[contains(text(),'some_text')]
Now I want to break it up in 2 parts as some random text is populating in between:
//*[contains(text(),'some'+ +'text')]
What I have done is to use '+' '+' to concatenate string in expression as we do in Java. Please suggest how can i get through this.
Upvotes: 2
Views: 101
Reputation: 89285
You can combine 2 contains()
in one predicate expression to check if a text node contains 2 specific substrings :
//*[text()[contains(.,'some') and contains(.,'text')]]
If you need to be more specific by making sure that 'text'
comes somewhere after 'some'
in the text node, then you can use combination of substring-after()
and contains()
as shown below :
//*[text()[contains(substring-after(.,'some'),'text')]]
If each target elements always contains one text node, or if only the first text node need to be considered in case multiple text nodes found in an element, then the above XPath can be simplified a bit as follow :
//*[contains(substring-after(text(),'some'),'text')]
Upvotes: 1