python - 如何处理 Selenium 中的奇怪字符? 'utf8' 编解码器无法解码位置 0 中的字节 0xc3 :

标签 python python-2.7 selenium unicode encoding

我试图在 selenium 中发送带有西类牙口音的键,我所做的是通过包含各种条目的数组发送字符串,这是它卡住的那一行。

["Electrodomésticos", "Otros electrodomésticos", ["sensorhumo.jpg"], "Sensor de humo inalámbrico independiente.", "-Frecuencia: 433Mhz. -Codigo de trabajo: 2262. -Alacance inalámbrico: 80 mts con línea de vista. ", "59000", "x", "x", "x", "x", "x", "x"],

当我发送这部分时:

“-Frecuencia:433Mhz。-Codigo de trabajo:2262。-Alacance inalámbrico:80 mts con línea de vista。”

此代码:

    descripcion=".//*[@id='field-description']"
    descripciontext=str(array3[i][x])
    x=x+1
    descripcionelement = wait.until(lambda driver: driver.find_element_by_xpath(descripcion))
    descripcionelement.send_keys(descripciontext) 

它抛出这条消息:

UnicodeDecodeError: 'utf8' codec can't decode byte 0xc3 in position 0: unexpected end of data

只有当我从这部分删除重音符号并尝试使用解码时它才有效。

最佳答案

当我尝试在表单内的选择选项中发送类似“España”的内容时,我自己也遇到了这个错误。

接受的答案是正确的,最好的方法是以 unicode 发送值。

我将在此处留下一个简短的简单代码,它以透明的方式转换发送的值,无论是 str 还是 unicode

def _convert(param):
    if isinstance(param, str):
        return param.decode('utf-8')
    else:
        return param

# both examples will work
parameter = 'España'
driver_element.send_keys(_convert(parameter)) 

unicode_parameter = u'España'
driver_element.send_keys(_convert(parameter))

关于python - 如何处理 Selenium 中的奇怪字符? 'utf8' 编解码器无法解码位置 0 中的字节 0xc3 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32910555/

相关文章:

python - 将带有两个参数的函数传递给 python 中的 filter()

selenium - Firefox Gecko 相当于 Chrome 的 --disable-dev-shm-usage 标志是什么

python - 索引超出范围错误: using Selenium to iterate page clicks in a list of elements over multiple webpages

python - 合并以 "/"python 分隔的列表项

python - "returns iterator"在python中是什么意思?

python - 读取文本文件并赋值

python - 内存中从 `raw_input()` 开始的字符串

RSelenium:抓取加载缓慢的动态加载页面

python 通过字符串值获取枚举不起作用

python - 如何 "zero"具有相等相邻条目的数组中的所有条目?