Shoaib Akhtar
Shoaib Akhtar

Reputation: 1403

Locator issue for element which has apostrophe (') in text

I have to find text -'To' email address is formatted incorrectly- using xpath. Below xpath is working well for me

(//*[contains(.,"'To' email address is formatted incorrectly.")])[5]

But when I put this xpath in my locator.

@FindBy(xpath="(//*[contains(.,"'To' email address is formatted incorrectly.")])[5]") public WebElement incorrectEmailFormatLabel;

I am getting warning error message in code. I know this is because of double quotes used("'To'..."), but if I change it to single quote(''To'...') I get this error message

invalid selector: Unable to locate an element with the xpath expression (//*[contains(.,''To' email address is formatted incorrectly.')])[5]

Let me know how to resolve it? I followed this post also, here xpath is not working for me.

(//*[contains(.,\" 'To' email address is formatted incorrectly.\")])[5]

Upvotes: 1

Views: 299

Answers (1)

Moshe Slavin
Moshe Slavin

Reputation: 5214

In the question, you provided the excepted answer shows using \ helps!

Try this:

@FindBy(xpath="(//*[contains(.,\"\'To\' email address is formatted incorrectly.\")])[5]") public WebElement incorrectEmailFormatLabel;

Hope this helps!

Upvotes: 1

Related Questions