在运行python自动化脚本时,无法解析含有中文的代码
例如:自动化脚本实现打开百度,搜索“鬼灭”
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
element = driver.find_element(By.NAME, 'wd')
element.send_keys('鬼灭\n')
解决方法:在字符串前加字母u
element.send_keys(u'鬼灭\n')
修改后运行成功
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
element = driver.find_element(By.NAME, 'wd')
element.send_keys(u'鬼灭\n')