2014-05-10 62 views
19

當我想要獲取URL頁面的源時,我在Windows7上遇到Selenium和PhantomJS的問題。 browser.page_source只返回<html><head></head></html>。我已經睡了browser.page_source之前,但它沒有幫助。Python:Selenium with PhantomJS空白頁面源

這是我的代碼:

from selenium import webdriver 
browser = webdriver.PhantomJS('phantomjs-1.9.7-windows\phantomjs.exe') 
url = 'myurl' 
browser.get(url) 
print browser.page_source 

在Linux上使用相同版本的PhantomJS它完美的作品。它也適用於Windows Server 2003.

+0

嘗試一些調試,捕捉例如 –

+2

截圖的截圖是空的,空白圖像。 我以爲這是PhantomJS的問題,我試圖用PyQt4的webkit加載頁面,但結果是一樣的,只是和標籤。 這是給我這個頭痛的URL:https://www.homesearch.com/browse?fulltextquery=miami+fl&page=0 –

+0

導航到某些_https_:// url時,我遇到了同樣的問題。使用'time.sleep(few_seconds)'或webdriver的[預期條件](http://selenium-python.readthedocs.org/en/latest/api.html#selenium.webdriver.support.expected_conditions.presence_of_element_located)等待一些元素在調用browser.get(url)後出現在頁面上' –

回答

31

默認情況下phantomjs使用SSLv3,但ssl中bug後的很多站點都會遷移到tls。這就是爲什麼你有空白頁面。 使用service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any']

browser = webdriver.PhantomJS('phantomjs-1.9.7-windows\phantomjs.exe', service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any']) 
+0

Upvoted這是因爲只使用ignore ssl錯誤對我不起作用,但添加ssl協議可以通過此評論幫助提供任何建議。 Kudos – freonix

+0

我希望我可以不止一次地對此進行升級 – valignatev

+0

請注意,'--ignore-ssl-errors'是危險的,除非您知道自己在做什麼,否則不應該使用它。另一種選擇可能會解決大多數問題。 –

8

使用service_args=['--ignore-ssl-errors=true']做了竅門!

browser = webdriver.PhantomJS('phantomjs-1.9.7-windows\phantomjs.exe', service_args=['--ignore-ssl-errors=true']) 
1
driverPhantom = webdriver.PhantomJS(driverLocation, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])  # initaling web driver for PhantomJs 

爲我工作。

0

增加如下的屏幕尺寸爲我工作:

driver = webdriver.PhantomJS(path2phantom, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any']) 
driver.set_window_size(2000, 1500)