1

我想测试无头火狐使用硒和下面的代码给出正确的结果。使用硒测试无头火狐,但它是抛出一个错误

From a fresh Ubuntu 14.04 install I did the following 

sudo apt-get install python-pip firefox xvfb 
pip install selenium pyvirtualdisplay 
useradd testuser 
And then in a python shell: 

from selenium import webdriver 
from pyvirtualdisplay import Display 
display = Display(visible=0, size=(800, 600)) 
display.start() 
driver = webdriver.Firefox() 
driver.get("http://askubuntu.com") 
print driver.page_source.encode('utf-8') 
driver.quit() 
display.stop() 

但是,如果使用在Django test.pyclass它不工作,并抛出一个错误,实现相同的功能。

class FirefoxHeadlessTestCase(LiveServerTestCase): 
    def setUp(self): 
     # start display 
     self.display = Display(visible=0, size=(1024, 768)) 
     self.display.start() 
     # start browser 
     self.driver = webdriver.Firefox() 

    def tearDown(self): 
     # stop browser 
     self.driver.quit() 
     super(FirefoxHeadlessTestCase, self).tearDown() 

     # stop display 
     self.display.stop() 

    # check if this test should be skipped 

    def test_example(self): 
     # run tests 
     print self.driver.get("http://askubuntu.com").page_source.encode('utf-8') 

Error: print self.driver.get("http://askubuntu.com").page_source.encode('utf-8') AttributeError: 'NoneType' object has no attribute 'page_source'

任何人有一个想法,我要去的地方错在这里?

回答

1

问题在于你的链接。请注意,你的Django代码略有不同

print self.driver.get("http://askubuntu.com").page_source.encode('utf-8') 

从其他Python代码

driver.get("http://askubuntu.com") 
print driver.page_source.encode('utf-8') 

不幸的是,驱动程序,以便在你的Django代码做了不能更改get方法不返回任何东西。您将需要与其他python代码一样的行。

+0

如果'driver.get(“http://askubuntu.com”)'为什么里面的Django相同的代码不返回任何东西正在恢复所有的HTML内容?自从我已经尝试过你建议我的方法后,我不认为问题在于链接。 – python

+0

根据文档firefox驱动程序.get不返回任何东西。都不是你所得到的和self.driver.get( “http://askubuntu.com”).page_source.encode( 'UTF-8')等于None.page_source.encode( 'UTF-8') – e4c5

+0

谢谢你,现在这个问题已经解决了 – python

相关问题