gpt4 book ai didi

javascript - 如何使用 PhantomJS (1.9.8) 将点击事件发送到 div 或按钮?

转载 作者:行者123 更新时间:2023-11-29 14:49:41 26 4
gpt4 key购买 nike

我不明白如何使用 sendEvent() 关闭模式或 PhanthomJS 元素.

例如,我尝试使用下面的代码或使用 CaperJS clickclickLabel 以及许多其他东西来做到这一点,但它不起作用。

var webpage = require('webpage').create();
var url = 'http://sourceforge.net/';
webpage.viewportSize = { width: 1280, height: 800 };
webpage.render("test1.png");

webpage.onLoadFinished = function(status) {
var coords = webpage.evaluate(function() {
var firstLink = document.querySelector('a.btn');
return {
x: firstLink.offsetLeft,
y: firstLink.offsetTop
};
});
console.log(coords.x);
console.log(coords.y);
webpage.sendEvent('mousedown', coords.x, coords.y ,'left');
};


webpage.open(url,function(){
webpage.render("test2.png");
phantom.exit()
});

这是我想跳过的元素。 enter image description here

最佳答案

offsetLeftoffsetTop 仅给出基于父元素的偏移量。您必须将所有元素偏移量相加到根节点。这至少是 CasperJS 所做的。

如果这个通知是静态的,你可以一次确定坐标,每次都使用它们。根据屏幕截图 (444,330) 似乎可以很好地替代 coords.x, coords.y

您也可以按照 here 所述简单地使用合成鼠标单击这就是 CasperJS 所做的,但结合了特定的位置。这是一个普通的合成点击:

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

此外,当页面加载时,您还有两个回调(onLoadFinishedwebpage.open 中的函数)。你应该只有一个,所以当调用其中一个时你不会遇到问题。点击后,最好用setTimeout稍等片刻,让页面发生变化再截屏。

关于javascript - 如何使用 PhantomJS (1.9.8) 将点击事件发送到 div 或按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27581193/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com