2

I'm querying values from database to enter in the input field using selenium. However, for certain values I get the following error:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 49: ordinal not in range(128)

I'm getting the error when the value to enter in the text field is something like 'Décor'. I understand that the issue with the character "é". How can I overcome this error?

Robot code:

*** Settings ***
Library    SeleniumLibrary
Library    DatabaseLibrary

*** Test cases ***
Test
    ${value}      Get value from database
    Input text    ${locator}    ${value}

*** Keywords ***
Get value from database
    ${queryResults}    Query    ${query}
    [Return]    ${queryResults}

Note: The error specifically occurs when inputting the text into the field(Step 2). Logging the same value to the console works fine.

Sameem
  • 831
  • 1
  • 8
  • 23
  • @DebanjanB - I did go through the answers in the question you have mentioned but I couldn't find a resolution for my problem. When I convert the **'D\xe9cor'** to a raw string it is entered into the input text as is. But I want to enter **"Décor"**. Problem is that the value from database is getting saved in the tuple as **'D\xe9cor'**. – Sameem Feb 21 '19 at 06:48
  • `\xe9` is `é` (`u'\xe9'`) encoded as cp1252 or latin-1. – snakecharmerb Feb 22 '19 at 07:03
  • @snakecharmerb - Yep. My question is , how do I get the value as ''é" instead of "\xe9"? – Sameem Feb 22 '19 at 09:49

1 Answers1

0

This being py2 my suspect would be the actual type of the variable you use (value); it may be a bytestring with the high-ascii characters in encoded form.

Just before using it in Input Text, convert it to unicode:

${value}=    Decode Bytes To String    ${value}

If that fails - or it doesn't produce the desired result, try with Convert To String:

${value}=    Convert To String    ${value}

I suspect the latter though will preserve the "offending" characters in their encoded form, e.g. "D\xe9cor". Do let me know in the comments, I'm quite curious :)

Todor Minakov
  • 19,097
  • 3
  • 55
  • 60
  • Decoding with the encoding set as UTF-8 returns error **UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 36: invalid continuation byte**. And 'Convert to String' converts it into a raw string. Same behavior as mentioned in the question comments. – Sameem Feb 21 '19 at 07:14
  • What's the type of `value`? Run this - `${the type}= Evaluate type($value)` - note the lack of curly brackets around the variable, and then `Log To Console ${the type}`. – Todor Minakov Feb 21 '19 at 07:28
  • . I'm getting even more curious now! – Sameem Feb 21 '19 at 07:32
  • For now I'm restricting fetching the data with the particular character (é). I know this is a workaround. This also vastly reduces my test data set since most of the data contains the character. But that's what I could think of now. – Sameem Feb 21 '19 at 07:36
  • 1
    In python2.7 bytearray/bytestring's type is 'str'; a unicode string is of type 'unicode'; so my suspicions are on track, let's try getting it to work for you :) – Todor Minakov Feb 21 '19 at 09:17
  • Oh. I didn't know that. Your inputs are highly helpful. Thanks! – Sameem Feb 21 '19 at 09:37
  • did you get any fix for this? I've been unable to get it to work! – Sameem Feb 25 '19 at 07:00