JDoodles
JDoodles

Reputation: 1

need to match xpath containing text with zero width space (ZWSP)

I'm trying to find a web element by its xpath. The web element is expected to contain text "blahblah". But somehow a zero width space (ZWSP) was incerted in the middle: blah&ZeroWidthSpaceblah This is why my xpath //a[contains(text(), 'blahblah')] does not match it. Is there xpath that would strip out/normalize the text value and remove all ZWSP?

I have tried //a[contains(text(), 'blah&ZeroWidthSpaceblah')] and it did not match. Even if it did, it owuld not be a good solution as it's hard to predict where ZWSP would be inserved by the application.

If not possible by the means of xpath definition itself, how would it be implemented in Java?

Upvotes: 0

Views: 369

Answers (1)

Michael Kay
Michael Kay

Reputation: 163458

Firstly, note that //a[contains(text(), 'blahblah')] is usually the wrong thing to write anyway. If you want to match an a element that contains blah as a substring, use //a[contains(., 'blah'). If you want to match an a element that contains blah in its entirety, use //a[.='blah']. The problem with text() comes when the a element contains child elements or comments: using '.' gives you the textual content of the element ignoring internal markup.

As to your question, //a[contains(translate(., '§', ''), 'blahblah')] will strip out any § characters from the content before doing the comparison. That simply leaves you with the problem of how to write a zero-width space in place of the §. As far as XPath itself is concerned, you can simply include a zero-width space character (U+200B) in the string (it won't be visible, of course). Your host language environment (Selenium in this case) might provide a better way of doing it, for example as \u200B - but I don't know Selenium.

Upvotes: 0

Related Questions