Search code examples
javascriptjavaautomationconsoleweb-console

How to write for loop for console log of web browser


I am currently learning some Automation.

I just wanted to know how to we write a for loop for a button in the webpage with the help of browser console.

document.querySelector('.glyphsSpriteCircle_add__outline__24__grey_9').click()

I need to this button to clicked until the button element is present.

Appreciate your help.


Solution

  • You continuously invoke a function with setTimeout until the element is found.

    function searchAndClick(selector, delay = 250){
        const el = document.querySelector(selector);
        if(el != null){
            el.click();
        } else {
            setTimeout(()=>searchAndClick(selector,delay),delay);
        }
    }
    searchAndClick('.glyphsSpriteCircle_add__outline__24__grey_9');