2015-02-09 94 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') 
相关问题