Skip to main content
 首页 » 编程设计

来自 django 文档错误 : 'f.QueryInterface is not a function' 的 Python selenium web 驱动程序示例

2024年08月27日40yyy_WW

我想为 Django 应用编写一些 Selenium 单元测试。我们已经有很多常规的 python 单元测试。我复制了 example selenium test from the Django docs (这只不过是 selenium 的“hello world”):

from selenium.webdriver.firefox.webdriver import WebDriver 
from django.test import LiveServerTestCase 
 
 
class MySeleniumTestCase(LiveServerTestCase): 
    @classmethod 
    def setUpClass(cls): 
        cls.selenium = WebDriver() 
        super(MySeleniumTestCase, cls).setUpClass() 
 
    @classmethod 
    def tearDownClass(cls): 
        cls.selenium.quit() 
        super(MySeleniumTestCase, cls).tearDownClass() 
 
    def test_simple(self): 
        self.selenium.get("/") 

但是,当我在本地或使用 CircleCI 运行它时(在云中测试即服务),我收到此错误:

====================================================================== 
ERROR: test_simple (proj.lib.tests.MySeleniumTestCase) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
  File "/home/ubuntu//proj/proj/lib/tests.py", line 1347, in test_simple 
    self.selenium.get("/") 
  File "/home/ubuntu/proj/venv/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 185, in get 
    self.execute(Command.GET, {'url': url}) 
  File "/home/ubuntu/proj/venv/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 173, in execute 
    self.error_handler.check_response(response) 
  File "/home/ubuntu/proj/venv/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 164, in check_response 
    raise exception_class(message, screen, stacktrace) 
WebDriverException: Message: u'f.QueryInterface is not a function' ; Stacktrace:  
    at FirefoxDriver.prototype.get (file:///tmp/tmpXXrLF6/extensions/fxdriver@googlecode.com/components/driver_component.js:9333) 
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpXXrLF6/extensions/fxdriver@googlecode.com/components/command_processor.js:11455) 
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpXXrLF6/extensions/fxdriver@googlecode.com/components/command_processor.js:11460) 
    at DelayedCommand.prototype.execute/< (file:///tmp/tmpXXrLF6/extensions/fxdriver@googlecode.com/components/command_processor.js:11402) 

这是Django 1.5.5,python 2.7,selenium python包2.42.1

有什么问题?如何让 selenium 工作?

请您参考如下方法:

get 方法需要一个完整的 URL。也就是说,URI 包括协议(protocol)和主机名/ip。

self.selenium.get("/") 
# becomes 
self.selenium.get("http://stackoverflow.com/questions/24011019/")