[英]How to fetch href links of the same class in Nightmare with jQuery
I am trying to fetch all href links
which are inside a separate divs with the same class name
using nightmare js... here is my code below.... 我正在尝试使用噩梦js来获取单独的div中具有相同
class name
所有href links
...这是下面的代码...
var Nightmare = require('nightmare');
var vo = require('vo');
vo(function* () {
var nightmare = Nightmare();
var title = yield nightmare
.goto('https://example.com')
.type('input[name="q"]', 'search term')
.click('input[value="some title"]')
.evaluate(function () {
//return $('.myclass').find('a').attr('href');
$('.myclass').find('a').each(function() {
return $(this).attr('href');
});
});
console.log(title);
yield nightmare.end();
})(function (err, result) {
if (err) return console.log(err);
});
below is my div contents... 以下是我的div内容...
<div class="myclass"><a href="example1.com">ex1</a>
</div>
<div class="myclass"><a href="example2.com">ex2</a>
</div>
<div class="myclass"><a href="example3.com">ex3</a>
</div>
It can fetch one link when i use return $('.myclass').find('a').attr('href');
当我使用
return $('.myclass').find('a').attr('href');
时,它可以获取一个链接return $('.myclass').find('a').attr('href');
but the each function does not work( returns null
).. any suggestion to fix this issue? 但是每个函数都不起作用(
returns null
)..有什么建议可以解决此问题?
I think you only need to use the class name as selector with "a" tag afterward: 我认为您只需要在以后使用带有“ a”标签的类名作为选择器:
$(".myClass a").each(function() {
return $(this).attr("href");
});
Here is a working example 这是一个有效的例子
i don't know nightmare.js
but if you want to collect all href
to an array do it like this: 我不知道
nightmare.js
但是如果您想将所有href
收集到数组,请执行以下操作:
.evaluate(function () {
var hrefs = [];
$('.myclass').find('a').each(function() {
hrefs.push($(this).attr('href'));
});
console.log(hrefs);
//return hrefs
});
.evaluate(function () {
var arr = [];
$('div.myClass > a').each(function(i) {
arr.push($(this).prop('href'));
});
return arr;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.