2015-02-09 23 views
0

我使用phantomjs webdriver從Python中使用硒。我在我的筆記本電腦上本地運行Selenium和我的服務器。當我運行我的測試:爲什麼Selenium與PhantomJS返回錯誤的page_source?

class TestNotLoggedIn(unittest.TestCase): 
    def setUp(self): 
     app.config['TESTING'] = True 
     app.config['CSRF_ENABLED'] = False 

     self.driver = webdriver.PhantomJS() 

    def test_frontpage(self): 
     driver = self.driver 
     driver.get('localhost:5000') 
     print driver.page_source 

它打印此HTML源:

<html><head></head><body></body></html> 

然而,當我去localhost:5000在瀏覽器中,我得到我的頁面的完整源:

!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>MyWebsite</title>etc. etc. 

有人知道我在做什麼錯嗎?

PS。我試過添加service_args=['--ignore-ssl-errors=true'])as suggested here,但這沒什麼區別。

+0

你可以嘗試在獲取頁面源之前與元素進行交互嗎?看起來該頁面尚未完全加載。你可以嘗試做一個隱式的等待來識別一個元素,然後抓取頁面源代碼。 – 2015-02-09 09:07:37

回答

1

原來PhantomJS需要http://也被定義。因此,對於未來的讀者(包括我自己):當獲得這樣的頁面時,它像一個魅力:

driver.get('http://localhost:5000') 
相關問題