Xpath with special character

3,745 views
Skip to first unread message

Thomas Vaidyan

unread,
Oct 19, 2012, 6:11:10 AM10/19/12
to webd...@googlegroups.com
Hi,

How to work with the xpath having special character. I am having an xpath with the value //*[@id='resInfo-0']. Selenium is not able to find the element.

Can you please help me

Regards
Thomas

anil kumar

unread,
Oct 19, 2012, 6:33:00 AM10/19/12
to webd...@googlegroups.com
Hi ,

We can build locator by using contains:

Xpath: //a[contains(@id,'resinfo')]
Css: css=a[id*='resinfo']

Thank you,
Anilkumar.G
> --
> You received this message because you are subscribed to the Google Groups
> "webdriver" group.
> To post to this group, send email to webd...@googlegroups.com.
> To unsubscribe from this group, send email to
> webdriver+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/webdriver?hl=en.

darrell

unread,
Oct 19, 2012, 11:00:33 AM10/19/12
to webd...@googlegroups.com
If the web site is not in UTF-8 or ISO-8859-1 then the source code will have special characters. If your source code is saved in UTF-8, ANSI or ISO-8859-1 then copying the string from say Firebug to your development environment will cause the string to get converted. That conversion is probably the problem you are encountering. Tools like Notepad++ have an encoding menu which tells you which encoding the text file has. If you view source and save it, you should be able to load the source code into Notepad++ (or similar) and see how it is encoded. You'd have to look at your source code editor and see if you can make it save the source code into the same format.

The other option is to use escape characters in the source code which would have the same numeric value as the string in the web page. For example, if I look at the source code with something like OD (object disassembly; a standard UNIX/Linux/Cygwin command) I might see the special character is \x01\x2F\x01\x12. So you can actually place "\x01\x2F\x01\x12" in place of the special characters in the string. Using this method is less readable however.

Darrell

P.S. avoiding the special characters is another option. You can use Anil's trick or you could see if there is something else on the tag which has not special characters and can still be a good locator.

P.P.S. many developers will make the customer facing text use the special characters but the attributes on a tag will be in UTF-8. For example, www.google.de is German but the id or name is in UTF-8.
Reply all
Reply to author
Forward
0 new messages