[英]Javascript - Confirm box to redirect user when they click on exit button
[英]Not able to click Button on a confirm box with javascript
我正在建立一個機器人。 我得到了這個html頁面,單擊了一個按鈕(type =“ submit”)。 完成后,它會彈出一個小窗口,如確認框。 我無法查看此確認框的來源,因為它未顯示在html代碼中。 但是從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>
我已經嘗試了幾種JavaScript代碼,但始終無法單擊“確認”按鈕。 似乎JavaScript代碼在確認框彈出之前運行,無法識別按鈕的ID和類。
我的代碼:
// Clicks the button on the html page, working OK
....{document.forms[0].sell.value = y-x;
document.querySelector('input[value="Calcular melhor oferta "]').click();
現在出現一個確認框,我嘗試使用下面的代碼單擊確認框中顯示的按鈕,但沒有成功:
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();}
好像此代碼在確認框出現之前運行。
我使用以下方法解決它:
var timeOff=1000;
setInterval(time, timeOff);
function time()
{ document.getElementById("pop").getElementsByClassName("evt-confirm-btn btn-confirm-yes")[0].click();}}
這會使代碼延遲1秒,足以在運行其余代碼之前運行小窗口。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.