【问题标题】:body = 'cmd=' + urllib_parse.quote_plus(unicode(verb).encode('utf-8')) returns "name 'unicode' is not defined"body = 'cmd=' + urllib_parse.quote_plus(unicode(verb).encode('utf-8')) 返回“名称'unicode'未定义”
【发布时间】:2014-10-30 15:28:15
【问题描述】:

当我在 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

----------------------------------------------------------------------

怎么了?有办法解决吗?

【问题讨论】:

    标签: python selenium python-3.4


    【解决方案1】:

    正如它所说,unicode 不再是 3.x 中的内置 - 相反,str 类型描述了一个 Unicode 字符串。

    您不应该收到此消息,因为它发生在 Selenium 代码中。这表明您未正确安装 Selenium - 您拥有 2.x 代码但正尝试从 3.x 使用它。

    快速查看文档表明 Selenium 支持 3.x - 但您需要卸载并重新安装它,并确保这次正确安装。

    【讨论】:

    • 感谢卡尔的回复。 Python3.4 与 Selenium 2.43 绑定。 Selenium 3.0 是相当新的。你如何 Selenium3.0 使用 Unicode 字符串而不是 unicode?有没有关于如何卸载 2.x 和安装 3.0 的文档?
    猜你喜欢
    • 2016-12-01
    • 2014-02-11
    • 2016-02-24
    • 2014-06-24
    • 2010-12-26
    • 2016-07-06
    • 1970-01-01
    • 2016-05-03
    • 2014-02-13
    相关资源
    最近更新 更多