本文介绍了机器人框架 - UnicodeDecodeError:'ascii' 编解码器无法解码位置 49 中的字节 0xe9:输入文本时序号不在范围内(128)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 selenium 从数据库中查询值以输入到输入字段中.但是,对于某些值,我收到以下错误:

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' 编解码器无法解码字节 0xe9 的位置49:序号不在范围内(128)

当要在文本字段中输入的值类似于 'Décor' 时,我收到错误消息.我了解字符 "é" 的问题.我该如何克服这个错误?

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?

机器人代码:

*** 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}

注意: 该错误具体发生在将文本输入到字段中时(第 2 步).将相同的值记录到控制台工作正常.

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

推荐答案

这是 py2 我怀疑将是您使用的变量的实际类型 (value);它可能是带有编码形式的高位 ascii 字符的字节串.

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.

Input Text中使用之前,将其转换为unicode:

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

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

如果失败 - 或者它没有产生想要的结果,请尝试使用 Convert To String:

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

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

我怀疑后者会以编码形式保留违规"字符,例如D\xe9cor".请在评论中告诉我,我很好奇:)

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

这篇关于机器人框架 - UnicodeDecodeError:'ascii' 编解码器无法解码位置 49 中的字节 0xe9:输入文本时序号不在范围内(128)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 04:47