2017-07-19 76 views
0

我安裝了PhantomJS,並且示例腳本the official Phantom website工作正常。PhantomJS截圖始終爲空

我試圖讓this screenshot example工作:

var page = require('webpage').create(); 
page.open('http://github.com/', function() { 
    page.render('github.png'); 
    phantom.exit(); 
}); 

但我不斷收到一個空白github.png文件稱重582KB

任何想法?

+1

你能上傳png文件嗎?如果它很大,它可能不是空白的。 – phihag

+1

也許URL需要是'https:// github.com /',因爲GitHub現在可以安全地提供服務。 –

+1

爲了避免這種猜測,我只是用最新的PhantomJS嘗試了這個確切的代碼,它對我來說工作正常。 – smarx

回答

0

您可能需要等待頁面實際加載和渲染。嘗試添加超時:

var page = require('webpage').create(); 
page.open('http://github.com/', function() { 
    setTimeout(function() { 
    page.render('github.png'); 
    phantom.exit(); 
    }, 500); 
}); 
+0

ced-b:仍然不起作用 –

1

@土木工程署-B的建議是明智的,但我不認爲你的問題將得到解決使用超時,因爲真正的問題是最有可能與SSL/TLS。 GitHub是一個HTTPS網站。所以如果你想獲得你的屏幕截圖,你可能需要在運行PhantomJS腳本時添加--ignore-ssl-errors=true選項。默認情況下,你應該得到一個狹窄的截圖,但你當然可以設置一個較大的視口:

var page = require('webpage').create(); 

page.viewportSize = { 
    width: 1920, 
    height: 1080 
}; 

page.open('https://github.com/', function() { 
    page.render('github.png'); 
    phantom.exit(); 
}); 
+0

Badacadabra:嘗試添加--ignore-ssl-errors = true並且仍然產生相同的png,尺寸 –

+0

這個腳本在我的電腦上工作正常(我使用相同的PhantomJS版本)。你的環境是什麼? – Badacadabra

0

曾與這個庫中的同一個問題:https://github.com/juliangruber/url-to-screenshot

設置sslCertificatesPath(「任意」)固定問題

new Screenshot('https://somerandomsite.com/') 
    .width(320) 
    .height(320) 
    .sslCertificatesPath('any') 
    .capture() 
    .then(img => { 
    // do stuff with img 
    })