我需要在一定的窗口大小的情况下拍摄截图。我在如何在协同欺骗中运行无头铬上读到了这篇博客文章,在开始使用无头铬上读到了这一页,但我想不出如何调整init窗口的大小。
在下面的所有选项中,我都试图通过--window-size,但没有一个有效:
--window-size=1280,1696
--window-size=1280x1696
--window-size 1280,1696
--window-size 1280x1696最糟糕的是,在做了一段时间之后,我的屏幕截图是空白的。在无头浏览器中是否保留了某种内存导致了这种情况?我还没有尝试重新启动我的Mac,这可能是下一个。
--那么,如果窗口大小为整页截图,有什么方法呢? In phantom.js,这是一件非常琐碎的事情。
我的代码在下面
const chromeLauncher = require('chrome-launcher');
const CDP = require('chrome-remote-interface');
const file = require('fs');
(async function() {
async function launchChrome() {
return await chromeLauncher.launch({
chromeFlags: [
'--window-size=1280,1696',
'--disable-gpu',
'--headless'
]
});
}
const chrome = await launchChrome();
const protocol = await CDP({
port: chrome.port
});
const {
DOM,
Network,
Page,
Emulation,
Runtime
} = protocol;
await Promise.all([Network.enable(), Page.enable(), Runtime.enable(), DOM.enable()]);
Page.navigate({
url: 'https://www.yahoo.com/'
});
Page.loadEventFired(async() => {
console.log('Start Page.loadEventFired');
const script1 = "document.querySelector('p').textContent"
const result = await Runtime.evaluate({
expression: script1
});
console.log(result.result.value);
const ss = await Page.captureScreenshot({format: 'png', fromSurface: false});
file.writeFile('output.png', ss.data, 'base64', function(err) {
if (err) {
console.log(err);
}
});
protocol.close();
chrome.kill();
});
})();如果我在命令行中这样做,它可以正常工作:
alias chrome-canary="/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary"
chrome-canary --headless --disable-gpu --screenshot --window-size=300,1696 https://www.yahoo.com/更新
我意识到无头Chrome金丝雀在Mac电脑上是非常不稳定的。如果我关闭终点站,重新启动它,有时它就能工作了。但是,在运行完全相同的脚本之后,它就会永远挂起(我一夜之间就离开了)。我今天刚刚更新了所有的内容( Canary,chrome,chrome界面),并继续进行调试。也许现在还不是黄金时段。
发布于 2017-08-09 17:40:46
好的,我刚刚学会了使用--window-size是行不通的。以下是作品:
添加:
Page.setDeviceMetricsOverride({
'width': 735,
'height': 2200,
'deviceScaleFactor': 1,
'mobile': false
});删除(导致无头铬冻结):
const script1 = "document.querySelector('p').textContent"
const result = await Runtime.evaluate({
expression: script1
});
console.log(result.result.value);https://stackoverflow.com/questions/45579242
复制相似问题