How to use unicode inside xpath string? (UnicodeEncodeError)

I am using xpath in Selenium RC via Python api.

I need to click an item whose text is "Submit" "

Here is the error I get:

In [18]: sel.click(u"xpath=//a[text()='Submit \xbb')]") --------------------------------------------------------------------------- UnicodeDecodeError Traceback (most recent call last) /Users/me/<ipython console> in <module>() /Users/me/selenium.py in click(self, locator) 282 'locator' is an element locator 283 """ --> 284 self.do_command("click", [locator,]) 285 286 /Users/me/selenium.py in do_command(self, verb, args) 201 body = u'cmd=' + urllib.quote_plus(unicode(verb).encode('utf-8')) 202 for i in range(len(args)): --> 203 body += '&' + unicode(i+1) + '=' + urllib.quote_plus(unicode(args[i]).encode('utf-8')) 204 if (None != self.sessionId): 205 body += "&sessionId=" + unicode(self.sessionId) UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 28: ordinal not in range(128) 
+7
python xpath unicode selenium-rc
source share
3 answers
 sel.click(u"xpath=//a[text()='Submit \xbb')]") 

You can write XPath expressions that contain Unicode characters .

For example:

//a[text()='Submit &#xBB;')]

+3
source share

I think you just need to change

 sel.click(u"xpath=//a[text()='Submit \xbb')]") 

to

 sel.click(u"xpath=//a[text()='Submit \xbb')]".encode('utf8')) 

This is because the error indicates that Selenium is trying to encode a Unicode object in a byte string (using the default codec for Python, that is, 'ascii' ), and that it crashes; at first explicitly encoding it so that it appears to be the correct codec ( 'utf8' , the default encoding in XML), so you should avoid this problem.

+1
source share

Does sel_click () assume unicode or utf-8 (byte) strings? The scary UnicodeEncodeError usually occurs when you try to pass the first, when the last is expected (or some other encoding).

I can’t try right now, but you can try

 "xpath=//a[text()='Submit \xc2\xbb')]" 

instead of your argument (which you get with .encode('utf-8') ) on it.

0
source share

All Articles