0

The element that I want to find with Selenium contains a text with both quotes and double quotes.

The text is:

"It's a traditional Valentines Day present and no one should spare money for it"

enter image description here

My auxilliary method:

private String preparePhraseForXpath(String phrase){
        return "concat('\"', 'It', '\'', 's a traditional Valentines Day present and no one should spare money for it', '\"')";
}

It is just a temporary stub method with a hardcoded phrase.

        String phraseForXpath = preparePhraseForXpath(phraseStr);
        try {
            WebElement tmpPhraseElement = seleniumService.findElement(driver,
                    0,
                    "//*[text() = '" + phraseForXpath + "']");

        ....

The element on the page:

<div id="react-aria6703854087-60" class="dc-TextArea__textarea dc-TextInput__textarea dc-TextInput__textarea_textAlign_left dc-TextInput__textarea_break_word dc-TextInput__textarea_with-highlight dc-Scrollbar dc-typography dc-typography_size_16-24 dc-typography_role_main dc-typography_color_gray" role="textbox" inputmode="text" aria-multiline="true" placeholder="Введите фразу или список фраз" data-testid="KeywordsMultipleInputs.Input0" aria-required="false" aria-invalid="false" aria-autocomplete="none" autocomplete="off" spellcheck="false" style="height: auto;" contenteditable="true">"It's a traditional Valentines Day present and no one should spare money for it"</div>

enter image description here

The problem: WebElementNotFoundException.

Could you tell me how to rewrite the hardcoded preparePhraseForXpath for the element to be found?

Kifsif
  • 3,477
  • 10
  • 36
  • 45

1 Answers1

0

Instead of a perfect match with text() as an alternative you can use partial match using contains() as follows:

In auxilliary method:

private String preparePhraseForXpath(String phrase){
    return "s a traditional Valentines Day present and no one should spare money for it";
}

Within the stub:

String phraseForXpath = preparePhraseForXpath(phraseStr);
try {
     WebElement tmpPhraseElement = seleniumService.findElement(driver, 0,"//*[contains(., '" + phraseForXpath + "')]");
     ....
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Unfortunately that will not suit me. It is to indiscriminate. The result may be confusing and prone to logical errors, which will be hard to reveal in future. But what's wrong with my concat and the use of it? – Kifsif Mar 24 '23 at 09:31