[英]Nightmarejs - How could I read table rows content?
I'm writing a little nightmare script that types names in a web form and reads results printed in a table. 我正在编写一个小噩梦脚本,该脚本在Web表单中键入名称并读取打印在表中的结果。 I've made the input typing already and get the table results like this:
我已经输入了输入内容,并得到如下表结果:
{ jQuery110205953448106032428: 124 }
So I don't know how to read rows from this element. 所以我不知道如何从该元素读取行。
My current script is: 我当前的脚本是:
const Nightmare = require('nightmare')
const nightmare = Nightmare({ show: true })
nightmare
.goto('https://rnped.segob.gob.mx/')
.click('small#leyendacomun')
.wait(2000)
.type('input#comun_c_nombre', 'María')
.type('input#comun_c_apaterno', 'Hernández')
.click('button#busca_comun')
.wait(2000)
.evaluate(function () {
return document.querySelector('table#t_comun')
// Here I select and return the <table>
})
.end()
.then(function (result) {
console.log(result)
// Here I get { jQuery110205953448106032428: 124 }
})
.catch(function (error) {
console.error('Error:', error);
});
How could I get the table rows from that query selector? 如何从该查询选择器中获取表行?
The result of evaluate can not be a reference to a DOM element like in your code. 评估的结果不能像您的代码中那样引用DOM元素。 Do all the data extraction inside your
evaluate()
function, eg, like this: 在您的
evaluate()
函数内部evaluate()
所有数据提取,例如,如下所示:
// ...
.wait(2000)
.evaluate(function () {
// get table and prepare result
const table = document.querySelector('table#t_comun'),
result = [];
// get rows
const rows = table.querySelectorAll( 'tr' );
for( let i=0; i<rows.length; i++ ) {
// get cells
let cells = rows[i].querySelectorAll( 'td' );
// get contents
let row = [];
for( j=0; j<cells.length; j++ ) {
row.push( cells[i].innerHTML );
}
// add to result
result.push( row );
}
// done, return result
return result;
})
.end()
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.