簡體   English   中英

重新加載 iframe 並盡可能多地單擊 iframe 內的按鈕

[英]reload iframe and click button inside iframe as much as I want

window.onhashchange = function () {
        window.setTimeout(function () {
            let frame = document.querySelector("body > iframe");
            if (frame !== null) {frame.replaceWith(frame);}
        }, 1000);
    }
//reload the iframe
var i = 0;
while (i < 100) {
    async function DStuff(){
        await document.querySelector("#Btn").click()
    }
    setTimeout(() => { DStuff() }, 3000)
    window.onhashchange();
    i++;
}
//click button

我想刷新 iframe 並盡可能多地單擊按鈕。 當我運行此腳本時,它不會循環。 此外,腳本應該在單擊按鈕后等待一秒鍾。

刪除循環並簡單地使用 setInterval:

window.onhashchange = function () {
        window.setTimeout(function () {
            let frame = document.querySelector("body > iframe");
            if (frame !== null) {frame.replaceWith(frame);}
        }, 1000);
    }
//reload the iframe

var i = 0;   
let myVar = setInterval(function(){ 
  i++;
  document.querySelector("#Btn").click();
  window.onhashchange();
  if (i === 100) {
    clearInterval(myVar);
  }
},3000);   

以上等待 3 秒,直到再次點擊,而在達到 100 次點擊后停止。 不用說,iFrame必須在同一個域,否則會因為跨站限制無法訪問。

如果你想在重新加載和點擊之間使用不同的時間延遲,你可以使用兩個遞歸調用對方的函數,用一個計數來停止循環:

 var totalIterations = 0; function clickButton() { console.log("clickButton called") // Your Click-button code here //document.querySelector("#Btn").click() // In 1 second, call the reload frame function setTimeout(reloadFrame, 1000) } function reloadFrame() { console.log("reloadFrame called") // Your Reload-frame code here: //let frame = document.querySelector("body > iframe"); //if (frame.== null) { frame;replaceWith(frame), } // If we haven't run 100 times yet, run the click button in 3 seconds if (totalIterations < 100) { setTimeout(clickButton, 3000) totalIterations++ } } // Call the function to start clickButton()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM