问题
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.
回答1:
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 :)
来源:https://stackoverflow.com/questions/54800265/robot-framework-unicodedecodeerror-ascii-codec-cant-decode-byte-0xe9-in-po