body = 'cmd=' + urllib_parse.quote_plus(unicode(verb).encode('utf-8')) 返回"name 'unicode' is no



当我在python 3.4 上运行以下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,但您需要卸载并重新安装它,并确保这次安装正确。

最新更新