简体   繁体   English

无法在带有JavaScript的确认框中单击按钮

[英]Not able to click Button on a confirm box with javascript

I'm building a bot. 我正在建立一个机器人。 I got this html page where I click on a button (type="submit"). 我得到了这个html页面,单击了一个按钮(type =“ submit”)。 When it does, it pops up a small window, like a confirm box. 完成后,它会弹出一个小窗口,如确认框。 I cannot view source of this confirm box as it is not shown on the html code. 我无法查看此确认框的来源,因为它未显示在html代码中。 But from the javascript console I get this code from the message box: 但是从javascript控制台,我从消息框中获取了以下代码:

<div class="confirmation-box" id="pop" role="dialog" aria-labelledby="confirmation-msg">
<div><div style="text-align: left"></div>
<div class="confirmation-buttons">
<button class="btn evt-confirm-btn btn-confirm-yes" aria-label'confirmar'="">Confirmar</button>
<button class="btn evt-cancel-btn btn-confirm-no" aria-label'cancelar'="">Cancelar</button>
</div></div></div>

I've tried several javascript codes but I'm never able to click the 'Confirmar' button. 我已经尝试了几种JavaScript代码,但始终无法单击“确认”按钮。 It seems like the javascript code runs before the confirmation box pops up and doesn't recognize the ID and Class of the button. 似乎JavaScript代码在确认框弹出之前运行,无法识别按钮的ID和类。

My code: 我的代码:

// Clicks the button on the html page, working OK
....{document.forms[0].sell.value = y-x; 
document.querySelector('input[value="Calcular melhor oferta "]').click();  

Now a confirmation box appears and I tried to use the next codes to click on the button that appears in the confirmation box, without sucess: 现在出现一个确认框,我尝试使用下面的代码单击确认框中显示的按钮,但没有成功:

document.getElementById("pop").getElementsByClassName("confirmation-buttons").getElementsByClassName("evt-confirm-btn btn-confirm-yes")[0].click();}

document.getElementsByClassName("evt-confirm-btn")[0].addEventListener("click", function(){console.log("hello");});

document.getElementsByClassName("evt-confirm-btn")[0].click();}

It seems like if this code runs before the confirmation box appears. 好像此代码在确认框出现之前运行。

I solve it using: 我使用以下方法解决它:

var timeOff=1000;
setInterval(time, timeOff);
function time()
{ document.getElementById("pop").getElementsByClassName("evt-confirm-btn btn-confirm-yes")[0].click();}}

This makes 1 second delay on the code, enough to run the small window before runing the rest of the code. 这会使代码延迟1秒,足以在运行其余代码之前运行小窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM