11

I'm using xpath in Selenium RC via the Python api.

I need to click an a element who's text is "Submit »"

Here's the error that I'm getting:

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)
GJ.
  • 5,226
  • 13
  • 59
  • 82

4 Answers4

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

It is possible to write XPath expressions that contain any Unicode characters.

For example:

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

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • Does Python provide a facility to encode Unicode like this? I don't see anything relevant in https://docs.python.org/3/library/codecs.html – ivan_pozdeev Oct 03 '18 at 14:32
  • @ivan_pozdeev, The right operand of the `=` comparison in the XPath expression is a string that anyone can specify in Python. Even if Python cannot work with Unicode, which I doubt, one can specify the string in the XPath expression and it is XPath that deals with the Unicode conversion – Dimitre Novatchev Oct 03 '18 at 14:40
  • Of course, I can write these symbols myself, but that's inconvenient, I'd rather write the characters normally and pass them to a conversion function. I'm asking if Python provides some stock function that would convert `u"»"` into `"»"`. – ivan_pozdeev Oct 03 '18 at 14:46
  • @ivan_pozdeev: Ask a regular question (not a comment) in the python tag – Dimitre Novatchev Oct 03 '18 at 17:27
  • `u` at the beginning of the xpath solved my problem, thanks! – Sohan Das Mar 29 '19 at 15:44
1

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'))

That's because the error indicates Selenium is trying to encode the Unicode object into a byte string (using the default codec for Python, that is, 'ascii') and that's what is failing; by explicitly encoding it yourself first, with what's presumably the right codec ('utf8', the default encoding in XML), you should therefore avoid this problem.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
0

Does sel_click() expect unicode strings or utf-8 (byte) strings? The dreaded UnicodeEncodeError usually happens when you try to pass in the first when the latter (or some other encoding) is expected.

I can't try it right now, but you could try

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

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

chryss
  • 7,459
  • 37
  • 46
  • (Hm, nevermind. I seem to have been typing this while Alex Martelli had posted an answer already...) – chryss Jun 12 '10 at 20:00
0

Im using selenium and roboframework I had this similar issue. I had an Xpath with a special charecter as below:

    xpath=(//a[contains(@href,'Ontkoppel cliënt')])[1]

and i had to replace with the ascii code and it worked fine.

    xpath=(//a[contains(@href,'Ontkoppel cli\u00EBnt')])[1]

Hope the example helps a little...

asha cr
  • 131
  • 3
  • I don't know what language this is, but it's not Python. The problem asked is Python-specific. – ivan_pozdeev Oct 03 '18 at 18:24
  • 1
    Hi Ivan, This question was with respect to using xpath in Selenium RC via the Python api so here in the above example i have stated the usage of unicode or special char inside the xpath so that to use them in Selenium RC via Python. :) – asha cr Oct 26 '18 at 08:23