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

相關問題