体= 'CMD =' + urllib_parse.quote_plus(UNICODE(动词).encode( 'UTF-8'))返回 “名称 '的unicode' 没有定义”
问题描述:
当我关于Python 3.4体= 'CMD =' + urllib_parse.quote_plus(UNICODE(动词).encode( 'UTF-8'))返回 “名称 '的unicode' 没有定义”
运行下面的Python代码# -*- coding: utf-8 -*-
import unittest, time, re
from selenium.selenium import selenium
class google(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*chrome", "https://www.google.com/")
self.selenium.start()
def test_google(self):
sel = self.selenium
sel.open("/?gws_rd=ssl")
sel.type("id=gbqfq", "")
sel.type("id=gbqfq", "test")
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
我有错误name 'unicode' is not defined:
======================================================================
ERROR: test_google (__main__.google)
----------------------------------------------------------------------
Traceback (most recent call last):
File "google.py", line 11, in setUp
self.selenium.start()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium-2.43.0-py3.4.egg/selenium/selenium.py", line 201, in start
result = self.get_string("getNewBrowserSession", start_args)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium-2.43.0-py3.4.egg/selenium/selenium.py", line 236, in get_string
result = self.do_command(verb, args)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium-2.43.0-py3.4.egg/selenium/selenium.py", line 215, in do_command
body = 'cmd=' + urllib_parse.quote_plus(unicode(verb).encode('utf-8'))
NameError: name 'unicode' is not defined
----------------------------------------------------------------------
有什么不对?有没有办法解决它?
答
就像它说的那样,unicode
不再是3.x中的内置 - 而是str
类型描述了一个Unicode字符串。
您不应该收到此消息,因为它发生在Selenium代码中。这表明你已经安装了不当的Selenium - 你有2.x代码,但是试图从3.x中使用它。
对文档的快速检查表明Selenium支持3.x - 但您需要卸载并重新安装它,并确保此时能正确安装。
感谢Karl的回应。 Python3.4与Selenium 2.43绑定。硒3.0是相当新的。 Selenium3.0如何使用Unicode字符串而不是unicode?有没有关于如何卸载2.x和安装3.0的文档? – Frank 2014-10-31 20:46:38