如何用python编码字符';什么是碎片?
我使用splinter为网站做功能测试。测试运行良好,只要我不输入特殊字符,就像德语Umlaute一样 如何调整脚本以测试包含这些字符的条目 这就是我迄今为止所尝试的:如何用python编码字符';什么是碎片?,python,python-2.7,encoding,utf-8,splinter,Python,Python 2.7,Encoding,Utf 8,Splinter,我使用splinter为网站做功能测试。测试运行良好,只要我不输入特殊字符,就像德语Umlaute一样 如何调整脚本以测试包含这些字符的条目 这就是我迄今为止所尝试的: # coding=utf-8 from splinter import Browser URL = "http://www.example.com" USER = raw_input("Username, please: ") # f.e. "Günther" with Browser('') as browser:
# coding=utf-8
from splinter import Browser
URL = "http://www.example.com"
USER = raw_input("Username, please: ") # f.e. "Günther"
with Browser('') as browser:
# Log in as administrator
browser.visit(URL)
browser.fill('user', USER)
这是我得到的错误日志:
Traceback (most recent call last):
File "test.py", line 47, in <module>
browser.fill('user', USER)
File "C:\Python27\lib\site-packages\splinter\driver\webdriver\__init__.py", line 230, in fill
field.value = value
File "C:\Python27\lib\site-packages\splinter\driver\webdriver\__init__.py", line 338, in _set_value
self._element.send_keys(value)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 293, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing})
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 370, in _execute
return self._parent.execute(command, params)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 164, in execute
response = self.command_executor.execute(driver_command, params)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 344, in execute
data = utils.dump_json(params)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\utils.py", line 30, in dump_json
return json.dumps(json_struct)
File "C:\Python27\lib\json\__init__.py", line 243, in dumps
return _default_encoder.encode(obj)
File "C:\Python27\lib\json\encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Python27\lib\json\encoder.py", line 270, in iterencode
return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x81 in position 0: invalid start byte
回溯(最近一次呼叫最后一次):
文件“test.py”,第47行,在
browser.fill('user',user)
文件“C:\Python27\lib\site packages\splinter\driver\webdriver\\uuuuu init\uuuuuu.py”,第230行,填写
field.value=value
文件“C:\Python27\lib\site packages\splitter\driver\webdriver\\ uuuuu init\uuuuu.py”,第338行,在\u set\u值中
self.\u元素。发送\u键(值)
文件“C:\Python27\lib\site packages\selenium\webdriver\remote\webelement.py”,第293行,位于发送键中
self._execute(Command.SEND_KEYS_TO_元素,{'value':键入})
文件“C:\Python27\lib\site packages\selenium\webdriver\remote\webelement.py”,第370行,在\u execute中
返回self.\u parent.execute(命令,参数)
文件“C:\Python27\lib\site packages\selenium\webdriver\remote\webdriver.py”,第164行,在execute中
响应=self.command\u executor.execute(driver\u command,params)
文件“C:\Python27\lib\site packages\selenium\webdriver\remote\remote\u connection.py”,执行中的第344行
data=utils.dump_json(参数)
文件“C:\Python27\lib\site packages\selenium\webdriver\remote\utils.py”,第30行,在dump\u json中
返回json.dumps(json_struct)
文件“C:\Python27\lib\json\\ uuuuu init\uuuuu.py”,第243行,转储
返回默认编码器编码(obj)
文件“C:\Python27\lib\json\encoder.py”,第207行,在encode中
chunks=self.iterencode(o,\u one\u shot=True)
文件“C:\Python27\lib\json\encoder.py”,第270行,在iterencode中
返回_iterencode(o,0)
UnicodeDecodeError:“utf8”编解码器无法对位置0中的字节0x81进行解码:起始字节无效
将用户
指定为unicode对象:
USER = u"Günther"
^
如果字符串来自外部源,请对其进行解码:
USER = USER.decode('<ENCODING-OF-THE-STRING>')
USER=USER.decode(“”)
是我遇到的最简单的unicode处理程序。很好的提示,但实际上我把示例简化了一点。我使用raw\u input()
获取USER
的变量。我更新了示例并尝试使用unicode(raw_input()),但这不是解决方案。如果您能更新您的答案,我将非常感激…@MartinBetz,尝试USER=raw_input(“用户名,请:”).decode('utf-8')
Hm,谢谢,这似乎是对的,但我收到另一条错误消息:`USER=raw_input(“用户名,请:”).decode('utf-8')File“C:\Python27\lib\encodings\utf_8.py”,第16行,在解码返回编解码器中。utf_8_解码(输入,错误,真)UnicodeDecodeError:“utf8”编解码器无法解码位置1中的字节0x81:无效开始字节`似乎您的终端编码不是utf-8。在python交互式shell中,下面的语句打印什么<代码>导入系统;打印系统标准编码。将utf-8
替换为打印的编码:USER=raw\u输入(“用户名,请:”)。解码('encoding\u printed')