CasperJS中的evaluate()中的DOM元素缺少click()函数

时间:2015-11-15 06:55:36

标签: javascript click phantomjs casperjs

与CasperJS相比,我在Chrome控制台的DOM元素上看到了不同的方法。我有以下我运行

var casper = require('casper').create({
  verbose: true,
  logLevel: "debug"
});

var url = 'http://casperjs.org/';

casper.start(url, function() {
  this.echo('sdfsdf');
});

casper.then(function() {
  var link = this.evaluate(function() {
    var b = document.querySelector(".page-header");
    var s = "";

    for (var m in b) {
      if (typeof b[m] === 'function') {
        s += JSON.stringify(m) + " ";
      }
    }
    return s;
  });
  console.log(link);
});

casper.run();

它产生

  

"的子" "的childNodes" " insertAdjacentElement" " insertAdjacentHTML"   " insertAdjacentText" "的setAttribute" "的getElementsByTagName"   "的getAttribute" " querySelectorAll" " webkitMatchesSelector"   " getElementsByClassName方法" "含有" " getBoundingClientRect"   "&的removeAttribute#34; " querySelector" " hasAttribute" " getAttributeNode"   " getAttributeNS" " getElementsByTagNameNS" " removeAttributeNS"   " getClientRects" " scrollByPages" "使用setAttributeNode" " setAttributeNS"   " hasAttributeNS" "模糊" " scrollIntoViewIfNeeded" " scrollByLines"   " setAttributeNodeNS" " removeAttributeNode" " getAttributeNodeNS"   "聚焦" " scrollIntoView" "的addEventListener" "使用appendChild" " cloneNode"   " removeChild之" " removeEventListener" " compareDocumentPosition"   "的insertBefore" " hasAttributes" "则isSupported" " isEqualNode"   " dispatchEvent" " isDefaultNamespace" " hasChildNodes" "正常化"   "的replaceChild" " isSameNode" " lookupPrefix" " lookupNamespaceURI"

另一方面,如果我在Chrome控制台中运行以下代码

var b = document.querySelector(".page-header");
var s = "";
for (var m in b) {
  if (typeof b[m] === 'function') {
    s += JSON.stringify(m) + " ";
  }
}

我得到以下内容。为什么CasperJS缺少click

  

""单击" "聚焦" "模糊" " hasAttributes" "的getAttribute"   " getAttributeNS" "的setAttribute" " setAttributeNS" "&的removeAttribute#34;   " removeAttributeNS" " hasAttribute" " hasAttributeNS" " getAttributeNode"   " getAttributeNodeNS" "使用setAttributeNode" " setAttributeNodeNS"   " removeAttributeNode" "最接近" "匹配" "的getElementsByTagName"   " getElementsByTagNameNS" " getElementsByClassName方法" " insertAdjacentHTML"   " createShadowRoot" " getDestinationInsertionPoints"   " requestPointerLock" " getClientRects" " getBoundingClientRect"   " scrollIntoView" " insertAdjacentElement" " insertAdjacentText"   " scrollIntoViewIfNeeded" " webkitMatchesSelector" "动画" "除去"   " webkitRequestFullScreen" " webkitRequestFullscreen" " querySelector"   " querySelectorAll" " hasChildNodes" "正常化" " cloneNode"   " isEqualNode" " compareDocumentPosition" "含有" " lookupPrefix"   " lookupNamespaceURI" " isDefaultNamespace" "的insertBefore" "使用appendChild"   "的replaceChild" " removeChild之" " isSameNode" "的addEventListener"   " removeEventListener" " dispatchEvent" "

1 个答案:

答案 0 :(得分:0)

如果你正在使用PhantomJS 1.x,则支持click(),但只支持<input><button>元素。有关详细信息,请参阅MDN。请注意,PhantomJS 1.x与Chrome 13相当,并且在Chrome 20中引入了对click()的完全支持。

此支持已扩展到PhantomJS 2中的所有元素类型,因为它有一个newer Webkit version

要么使用CasperJS&#39; click()

casper.click(selector);

或PhantomJS的众多变通办法之一:PhantomJS; click an element