PhantomJS;单击元素 [英] PhantomJS; click an element

查看:181
本文介绍了PhantomJS;单击元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在PhantomJS中点击元素?

  page.evaluate(function(){
document。 getElementById('idButtonSpan')。click();
});

这会给我一个错误:undefined is not a function ...



如果我改为

  return document.getElementById('idButtonSpan'); 

然后打印,



它打印[object object],所以元素确实存在。



元素充当一个按钮,但它实际上只是一个span元素, p>

我可以得到这个按钮单击使用Casper,但Casper有其他限制,所以我回到PhantomJS。

解决方案

.click()不是标准。您需要创建一个事件并分派它:

  function click(el){
var ev = document.createEvent (MouseEvent);
ev.initMouseEvent(
click,
true / * bubble * /,true / * cancelable * /,
window,null,
0, 0,0,/ * coordinates * /
false,false,false,false,/ *修饰键* /
0 / * left * /,null

el.dispatchEvent(ev);
}


How do I click an element in PhantomJS?

page.evaluate(function() {
    document.getElementById('idButtonSpan').click();  
});

This gives me an error "undefined is not a function..."

If I instead

 return document.getElementById('idButtonSpan');

and then print it,

then it prints [object object], so the element does exist.

The element acts as a button, but it's actually just a span element, not a submit input.

I was able to get this button click to work with Casper, but Casper had other limitations so I'm back to PhantomJS.

解决方案

.click() is not standard. You need to create an event and dispatch it:

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);
}

这篇关于PhantomJS;单击元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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