selenium中sendkeys()方法输入中文报错之解决方案

使用python编写selenum,python的编码为utf8,直接打印中文都是正常的,但是在使用selenium的方法sendkeys()发送中文时,会报错提示UnicodeDecodeError: ‘utf8’ codec can’t decode byte 0xe6 in position 0: unexpected end of data
selenium中sendkeys()方法输入中文报错之解决方案_第1张图片

经过测试发现,sendkeys()发送中文时需要明确指出其编码。以下是我的测试两个解决方案,仅供参考使用。

#!/usr/bin/python
# coding=utf-8
#引去类
from selenium import webdriver
import time
#drive引用
driver = webdriver.Firefox()
#百度登录按钮的输入框使用class进行定位
driver.get(“http://www.baidu.com“)
time.sleep(10)
#方式1,在中文前加入u
# driver.find_element_by_class_name(“s_ipt”).send_keys(u’测试’)

#方式2,使用decode()方法
str = ‘测试’
print str
driver.find_element_by_class_name(“s_ipt”).send_keys(str.decode(‘utf-8’))
driver.find_element_by_id(‘su’).click()

driver.quit()

你可能感兴趣的:(selenium,学习进阶,解决方案,python,selenium,编码)