我需要用casperjs提取所有id值 [英] I need to extract all id values with casperjs

查看:60
本文介绍了我需要用casperjs提取所有id值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

id = casper.evaluate(function() { 
  return Array.prototype.map.call(document.querySelectorAll("image"), 
  function(e) {return e.getElementById();});});

this.echo(id);


推荐答案

-更新-

如果要获取包含特定的元素,只需使用辅助函数来抓取您要查找的元素。

If you want get elements containing a specific class just use a helper function to scrape the elements you are looking for.

示例:

var casper = require('casper').create();
var ids = [];

function getIdsByClassValue() {
  // use your selector here eg. '.image'.
  var elems = document.querySelectorAll('input[type="submit"]');
  return Array.prototype.map.call(elems, function (e) {
    // change to the attribute you are looking for.
    return e.getAttribute('value')
  });
}

casper.start('https://www.google.com/');

casper.then(function () {
  ids = this.evaluate(getIdsByClassValue);
});

casper.run(function() {
  this.echo('\n - ' + ids.join('\n - ')).exit();
});

您可以使用 getElementsAttribute 来做到这一点。

You can use getElementsAttribute to do that.


.getElementsAttribute

签名:getElementsAttribute(字符串选择器,字符串属性)

Signature: getElementsAttribute(String selector, String attribute)

检索每个元素上与提供的选择器匹配的一个属性:

这里是一个示例。

var ids = [];

var casper = require('casper').create();

casper.start('https://google.com/', function() {
    this.wait(1000, function() {
        ids = this.getElementsAttribute('*', 'id')
          .filter(function(id) {
              return id.length > 0;
          })
    });
});

casper.then( function() {
  this.echo('\n - ' + ids.join('\n - ')).exit();
})

casper.run();

这篇关于我需要用casperjs提取所有id值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆