[英]Nightmarejs - How could I read table rows content?
我正在編寫一個小噩夢腳本,該腳本在Web表單中鍵入名稱並讀取打印在表中的結果。 我已經輸入了輸入內容,並得到如下表結果:
{ jQuery110205953448106032428: 124 }
所以我不知道如何從該元素讀取行。
我當前的腳本是:
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);
});
如何從該查詢選擇器中獲取表行?
評估的結果不能像您的代碼中那樣引用DOM元素。 在您的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.