XPath text/replace to find text which may contain soft-hyphen

516 views Asked by At

Searching for a text: Bescheinigungen my XPath should also return elements which include a soft-hyphen like: Beschei\u00ADnigungen.

I tried this:

//*[text()[replace(., "\u00AD", "")="Bescheinigungen"]]

Doesn't work. Need some help, please.

1

There are 1 answers

0
Moritz Schmitz v. Hülst On BEST ANSWER

Okay, had to use some 'helper' code to make it work:

public static WebElement findByText(WebDriver driver, String text) {
  List<WebElement> elements = driver.findElements(By.xpath("//*[text()]"));
  return elements.stream().filter(element -> {
    String elementText = element.getText();
    if (elementText != null && !elementText.isEmpty()) {
      return text.equals(elementText.replace("\u00AD", ""));
    }
    return false;
  }).findFirst().orElseThrow(NotFoundException::new);
}