python - body = 'cmd=' + urllib_parse.quote_plus(unicode(verb).encode ('utf-8' )) 返回 "name ' unicode' 未定义”

标签 python selenium python-3.4

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

我遇到错误名称'unicode'未定义:

======================================================================
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 - 但您需要卸载并重新安装它,并确保这次正确。

关于python - body = 'cmd=' + urllib_parse.quote_plus(unicode(verb).encode ('utf-8' )) 返回 "name ' unicode' 未定义”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26657067/

相关文章:

python - 将类似dict的字符串转换为JSON

java - 如何在测试运行时保持 'Developer Tool' 窗口打开

Python3.4 : for each line in text

python - 我正在尝试将时间列转换为日期时间,但该列看起来像这样 '2020-01-07T22:24:20.000Z'

python - 将大文件加载到 Flask 中

python - 如何使用python dash循环显示多个图像

python - 文件未找到错误 : [WinError 2] python 3. 4

javascript - 守夜人 : Using custom commands to iterate over all select tags

java - webdriver,捕获@Test(超时= 10000)

python - 如何对多个字段分组的数量求和?