- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在运行一个 nightmare.js 脚本,我试图在其中截取页面上多个元素的屏幕截图。
第一个 元素被捕获得很好,但折叠下方的所有其他元素都被捕获为零长度。我正在努力调试这个问题。任何帮助将不胜感激。
基本上,此脚本遍历页面并选择页面上所有 与选择器匹配的元素。然后,它使用 async
收集响应并返回一个对象缓冲区。问题是折叠下方的元素不会被截屏(缓冲区长度最终为零)。我尝试 wait()
并滚动到该元素,但到目前为止我还没有成功。
import * as Nightmare from 'nightmare'
import * as vo from 'vo'
import * as async from 'async'
import * as fs from 'fs'
const urls:String[] = [
'https://yahoo.com/'
]
Nightmare.action('snap', function(selector:String, done:Function) {
const self = this;
this.evaluate_now(function (selector) {
return Array.from(document.querySelectorAll(selector))
.map((ele:Element) => {
if (ele) {
const rect = ele.getBoundingClientRect()
const r:Function = Math.round
return {
x: r(rect.left),
y: r(rect.top),
width: r(rect.width),
height: r(rect.height)
}
}
})
}, function(err, clips) {
if (err) return done(err)
if (!clips) return done(new Error(`Selector not found`))
let snaps = []
const snap = (clip, cb) => {
self
.scrollTo(clip.y - clip.height, clip.x)
.screenshot(clip, cb)
.run()
}
async.mapSeries(clips.reverse(), snap, (err, res) => {
done(err, res)
})
}, selector)
})
const scrape = (url) => {
const nightmare = Nightmare({
show: true
});
nightmare
.goto(url)
.snap('.navbar')
.end()
.then((buffers:Buffer[]) => {
buffers.forEach((data, index) => {
fs.writeFileSync(`images/navbar-${index}.png`, data)
})
})
}
urls.forEach(scrape)
最佳答案
实际上,screenshot() 函数从可见屏幕获取坐标。
例如,如果任何元素的 (x,y) 是 (10, 1000) 并且您的窗口大小是 (800,600) 那么您可以滚动 (900:element.y, 0) 然后在 (element.y-scroll. y=100,元素.x)
我终于让代码工作了:
const Nightmare = require('nightmare');
const fs = require('fs');
const nightmare = Nightmare({
show: true,
openDevTools: true,
});
nightmare.goto('https://in.news.yahoo.com/')
.wait(1000)
.evaluate(getBounds, '.Cf')
.then(function(rects) {
console.log(rects);
function getScreenshot(rects, index) {
if (index == rects.length) return;
nightmare.scrollTo(rects[index].y, 0)
.screenshot(__dirname + '/images/navbar' + index + '.png', {
//60 is height of the top element which remains
x: rects[index].x-10,
y: 60,
width: rects[index].width+30,
height: rects[index].height +60
})
.then(function() {
console.log("Calling next. " + index);
getScreenshot(rects, index + 1);
}).catch(function(err) {
console.log(err);
})
};
getScreenshot(rects, 0);
})
.catch(function(err) {
console.log(err);
});
function getBounds(selector) {
var elements = document.querySelectorAll(selector);
if (elements && elements.length > 0) {
var arr = [];
const r = Math.round;
for (var ii = 0; ii < elements.length; ii++) {
var rect = elements[ii].getBoundingClientRect();
arr.push({
x: r(rect.left),
y: r(rect.top),
width: r(rect.width),
height: r(rect.height)
})
}
console.log("Elements found: ", arr.length);
return arr;
}
return null;
}
关于javascript - Nightmare.js 屏幕截图缓冲区长度为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44580348/
好吧,我用 Nightmare 来做一些测试任务,对于基于电子和phatomjs的代码来说是如此的好。 现在,我有一个问题,我该如何通过 Nightmare 上传图像文件? 好吧,我找到了一个名为ni
我找不到这方面的任何信息,主要是因为一旦我在同一句话上输入带有“Nightmare ”和“IE”的google搜索,您就会知道... 有没有办法像在IE兼容模式下一样运行nightmare.js? 最
我试图从网站中提取数据,我想在 10 分钟后循环查看数据是否已更改。我认为将我的功能打包在模块中是一个好主意,但现在我坚持将我当前的 Nightmare 实例从登录模块转移到数据模块。 开始.js v
我很难过要在Travis CI上运行它。它会间歇性地工作,但 Nightmare 通常会因以下错误而失败: nightmare:log did-get-response-details [{},f
使用nightmarejs如何在文件输入中输入文件并进行确认? 它用于附件的自动化(例如电子邮件)。我的意思是输入文件是这样的: 最佳答案 我所知道的使用Nightmare进行上传的唯一方法是使用de
我想抓取并通过页面中的每个herf获取html,然后输出到csv。现在我第一次使用 Nightmare 。所以我对未定义的段落有疑问。 var Nightmare = require('nightma
var Nightmare = require('nightmare'); var nightmare = Nightmare({ show: true }); var fs = require('
我目前正在开发一个程序来自动化某个网站上的任务(简化代码如下)。我很好地登录,然后等待页面加载,但是wait函数永远不会结束。我尝试使用wait(200),但是评估函数返回一个空数组。我100%确信网
我尝试在F12时单击包含这些详细信息的按钮 Show all results for 'lundhags' 我已经复制了xpath并且有这样的代码=>I.click('//*[@id="9"]');
我在javascript中使用Nightmare创建网络抓取工具,但是评估功能未返回正确的信息。似乎它能够返回原始类型(int,字符串等),但不能返回“querySelectorAll”中的元素列表。
我正在尝试NightmareJS进行一些我想完成的自动化操作。我遇到的一个问题是, Nightmare/Electron 无法解析adidas网站,但是它可以解析并访问任何其他网页。 This is
我想使用 Nightmare JS 通过检查状态代码 200 来确定页面是否正在加载。我查看了 goto 选项,但无法弄清楚。大家有什么想法吗? var Nightmare = require('ni
我刚刚开始使用 Nightmare 。我在我的网页上使用它用于测试目的。 我有以下用户界面。 menu tab1 | tab2 | tab3 ......(dynamically generated
如何使用 nightmare-iframe 插件访问具有随机生成名称的 iframe? 我试过了 var name = yield nightmare .goto(defaultUrl) .c
如何在服务器上通过pm2或类似的东西运行nightmarejs,因为 Nightmare 使用电子,而电子现在需要图形,我正在使用xvfb-run npm start ,但是这个 sol° 需要我通过
下面的代码片段是来自 https://github.com/segmentio/nightmare 的示例 const Nightmare = require('nightmare') const n
我知道在任何环境中处理日期都可能会非常令人困惑,但我正处于一场噩梦中,这个函数本应该是一项微不足道的工作。我想以不同的方式操作某些日期,但出现错误或错误的结果。 下面我将报告一个非常简单的例子来测试执
我有以下代码: for(var i = 0; i { return true; }) } 我正在 Electron 应用程序中使用它。然而,这会执行异步,我会立
我正在尝试使用 github.com/segmentio/nightmare 自动化 2 个站点 例子: 我想继续访问 site_1(临时电子邮件服务)并获得电子邮件。 然后转到 site_2 并填写
我正在尝试使用 Nightmare 抓取网页,但想等待 #someelem 出现,前提是它确实存在。否则,我希望 Nightmare 继续前进。如何使用 .wait() 完成此操作? 我不能使用 .w
我是一名优秀的程序员,十分优秀!