无法点击按钮

Unable to click button

本文关键字:按钮      更新时间:2023-09-26

我对Javascript非常陌生,想知道你是否可以帮助,我在我的脚本中使用waitForKeyElements,但是我不能让它在脚本中单击网站上的按钮。

/*- This is the html of the button i'd like to click when it appears
<div id="battleInfo">
<center>You are out of autos 
<a href="#" onclick="phoenixtales.battle.restartBattleAuto();">Restart</a>
</center>
</div>
*/

// ==UserScript==
// @name         Autoscript
// @version      0.1
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require      http://gist.github.com/raw/2625891/waitForKeyElements.js
// @include      http://tpt-rpg.com/auto_battle.php
// @grant        GM_addStyle
// ==/UserScript==
function clickattack (jNode) {
var clickEvent = document.createEvent
('MouseEvents');
clickEvent.initEvent('click', true, ture);
jNode[0].dispatchEvent (clickEvent);
}
waitForKeyElements (
"button[onclick*='phoenixtales.battle.restartBattleAuto()']"",
clickattack);

您正在将事件附加到按钮上,但它实际上被定义为附加到html中的锚。所以你可以试试

"a[onclick*='phoenixtales.battle.restartBattleAuto()']"",

代替

"button[onclick*='phoenixtales.battle.restartBattleAuto()']"",

,但为什么不给你的锚点一个id,这样你就可以使用id来找到它?像这样:

<a id="restartAnchor" href="#" onclick="phoenixtales.battle.restartBattleAuto();">Restart</a>

"#restartAnchor[onclick*='phoenixtales.battle.restartBattleAuto()']"",

免责声明:我不熟悉waitForKeyElement,但它似乎是使用css选择器查找的东西。