0

I am trying to locate a UI popup using xpath and inner text. The popup html is like this:

Hello ? {some elements} What would you like to do today {more elements} Play or Die ? {Yes button, No button}.

I want to get this element by using something like this:

//div[contains(@innerText, 'Hello ? What would you like to do today Play or Die ?')]

How do I do this ? Or is there a better way to find this popup ? There are no Ids or permanent classes here. Moreover the DOM structure is variable.

1
  • will appreciate a vote-up on the answer. Thanks! Commented May 26, 2017 at 15:34

2 Answers 2

3

You should be able to use xpath ver2's matches function:

//div[matches(text(), 'Hello ?\w+ What would you like to do today \w+')]

Which allows regex.

Sign up to request clarification or add additional context in comments.

Comments

1

Assuming that the interleaving elements contain no inner text, you can use . instead of @innerText to get concatenation of all text nodes within context element (the div in this case). Combine . with normalize-space() to remove leading and trailing whitespace characters, as well as to normalize consecutive whitespace characters into single space. With normalize-space() the . can be removed as it is the default parameter :

//div[contains(normalize-space(), 'Hello ? What would you like to do today Play or Die ?')]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.