PhantomJS; 单击一个元素

use*_*003 80 javascript click phantomjs

如何单击PhantomJS中的元素?

page.evaluate(function() {
    document.getElementById('idButtonSpan').click();  
});
Run Code Online (Sandbox Code Playgroud)

这给了我一个错误"未定义不是一个函数......"

如果我改为

 return document.getElementById('idButtonSpan');
Run Code Online (Sandbox Code Playgroud)

然后打印出来,

然后它打印[object object],因此元素确实存在.

该元素充当按钮,但它实际上只是一个span元素,而不是提交输入.

我能够点击这个按钮点击Casper,但Casper有其他限制,所以我回到了PhantomJS.

小智 67

.click()不标准.您需要创建一个事件并发送它:

function click(el){
    var ev = document.createEvent("MouseEvent");
    ev.initMouseEvent(
        "click",
        true /* bubble */, true /* cancelable */,
        window, null,
        0, 0, 0, 0, /* coordinates */
        false, false, false, false, /* modifier keys */
        0 /*left*/, null
    );
    el.dispatchEvent(ev);
}
Run Code Online (Sandbox Code Playgroud)

  • 用jQuery包装`el`给你这个定义的点击.它可以在您的浏览器和Phantom中运行.`$(EL).点击()` (9认同)
  • 在经过近12个小时的不断斗争之后,欢乐的泪水!嗅探嗅探! (3认同)

The*_*Sky 34

作为@ torazaburo的回应的替代方案,你可以HTMLElement.prototype.click在PhantomJS中运行时存根.例如,我们使用PhantomJS + QUnit来运行我们的测试,在我们中qunit-config.js我们有这样的东西:

if (window._phantom) {
  // Patch since PhantomJS does not implement click() on HTMLElement. In some 
  // cases we need to execute the native click on an element. However, jQuery's 
  // $.fn.click() does not dispatch to the native function on <a> elements, so we
  // can't use it in our implementations: $el[0].click() to correctly dispatch.
  if (!HTMLElement.prototype.click) {
    HTMLElement.prototype.click = function() {
      var ev = document.createEvent('MouseEvent');
      ev.initMouseEvent(
          'click',
          /*bubble*/true, /*cancelable*/true,
          window, null,
          0, 0, 0, 0, /*coordinates*/
          false, false, false, false, /*modifier keys*/
          0/*button=left*/, null
      );
      this.dispatchEvent(ev);
    };
  }
}
Run Code Online (Sandbox Code Playgroud)


sto*_*roz 15

它不漂亮,但我一直在使用它来允许我使用jQuery进行选择:

var rect = page.evaluate(function() {
    return $('a.whatever')[0].getBoundingClientRect();
});
page.sendEvent('click', rect.left + rect.width / 2, rect.top + rect.height / 2);
Run Code Online (Sandbox Code Playgroud)

但你总是可以替换$(s)[0]使用document.querySelector(s),如果不使用jQuery.

(它确实依赖于视图中的元素,即你的viewportSize.height足够大).

  • 尝试了所有可能的方法,这是我尝试点击`tr`时唯一有用的选项. (2认同)

Job*_*ohn 10

希望以下方法有用.它在版本1.9中适用于我

page.evaluate(function(){
    var a = document.getElementById("spr-sign-in-btn-standard");
    var e = document.createEvent('MouseEvents');
    e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    a.dispatchEvent(e);
    waitforload = true;
});
Run Code Online (Sandbox Code Playgroud)

这对我有用.希望这对其他人也有用


ssh*_*haw 6

有了1.9.2这个工作对我来说,被触发的点击处理程序:

var a = page.evaluate(function() {
    return document.querySelector('a.open');
});

page.sendEvent('click', a.offsetLeft, a.offsetTop);
Run Code Online (Sandbox Code Playgroud)

  • @unludo我的例子使用了<a>,它有效:) (2认同)

Hap*_*888 6

使用简单的JavaScript evaluate,如下所示:

page.evaluate(function() {
    document.getElementById('yourId').click();
});
Run Code Online (Sandbox Code Playgroud)