0

I have been running Automated Tests apart of builds for some times now, and a series of tests surrounding email validation kept failing. The issue I have found is due to a javascript error that is thrown explaining that Selenium cannot search for a '@' in text within an XPath.

I came across this SO Question which nicely confirmed to me that characters cannot be searched for within text via XPath in Selenium as standard.

My question is, has anyone found a way around this?

For example, I would like to be able to find an element with the following text field:

<h1>"personA@email.com.au.personB@email.com.au - The separator '@' is either in an invalid position or is missing from the address."</h1>

And my current XPath (clearly won't work with the presence of symbols) that throws a javascript error is (where errorText is just the above string):

"//*[contains(text(), '" + errorText + "')]"

And just for fun, if the javascript error helps:

[14/01/2013 -  14:35:00.1820111]    :    InvalidSelectorError: Unable to locate an element with the xpath expression //*[contains(text(), 'personA@email.com.au.personB@email.com.au - The separator '@' is either in an invalid position or is missing from the address.')] because of the following error:
Error: Bad token: @

[EDIT]

I have looked into the SO Question that suggests I need to surround my input text with double quotes, however doing this still causes the above javascript error to throw. I still am under the impression the @ symbol is the issue here and can see no obvious way to search via text including it.

Community
  • 1
  • 1
Nashibukasan
  • 2,028
  • 23
  • 37

2 Answers2

1

You problem is absolutely not related to @, but rater to "how to escape single/double quotes in XPAth".

Your XPath looks like ...contains(text(),'foo'@'bar').... One can easily see that string to search is invalid as it consist of 2 parts combined with @ in the middle, which makes not sense in XPath.

Fix: properly escape all characters to avoid equivalent of "SQL injection" when building XPath with string concatenations - covered in Special Character in XPATH Query . In your case outer double quotes should be enough (as sample string does not contain mix of single and double quetes. Otherwise - look at sample in the linked question and build complete search string with concat function:

"...contains(text(), \"'foo'@'bar'\")...
Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • I have removed my previous comment because, while the post you linked me too fixed my problem when I used testing strings, my actual input string still fails when surrounded by '"'s due to @ symbols. Will update my question now. – Nashibukasan Jan 16 '13 at 04:46
1

The @ symbol is not the issue here. It's a red herring. The issue is that your errorText value contains single quotes, so the resulting XPath, after concatenation, is (abbreviating for clarity):

//*[contains(text(), 'personA@Email ... separator '@' is ... address.')

As you can see, the ' before @ is ending the string literal, and the next character after that is @, which is why the error is complaining about @ being where it shouldn't be. If errorMessage instead contained 'hello', the error message would probably complain about the h.

A potential workaround for this is to replace all the single quotes in both text() and the source value with some other character or remove them during the comparison. Replacing them with double quotes could be a sensible choice:

"//*[contains(translate(text(), \"'\", '\"'), '" + errorText.replace("'", '"') + "')]"

Or to remove the single quotes during the comparison:

"//*[contains(translate(text(), \"'\", ''), '" + errorText.replace("'", '') + "')]"

Could you give that a try?

JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • I had not tried removing the quotations around the @, I shall try this when I get to my other machine, thanks! – Nashibukasan Jan 16 '13 at 09:56
  • Your code sample helped me greatly. I like the fact you can get XPath to perform a method on a string before attempting to match it. Thanks greatly! – Nashibukasan Jan 17 '13 at 02:24