2017-07-19 842 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 
    })