0

I have a small chrome extension as a beginner project and I have the following situation.

Lets pretend I have a website with a search button. When the button is pressed, two options can come up:

  1. there is a search result with a button called "buy"
  2. there is no search result and so is no button "buy"

This is the relevant part of code I have:

        mouseEventClick('#search'); // Search btn

        await sleep(250); // hard coded sleep timer

        // here I need a way to wait for the #buy button is clickable.


        try {
            mouseEventClick('#buy'); // click buy btn
            
        } catch {
            await sleep(750);
            mouseEventClick('#back'); // no result - go back to search mask
        }

I would instead of the await sleep(250) I would like to make it that way that the app is waiting until the button #buy is loaded in the DOM and clickable. Then it should go into the try code block.

But I do not want it to wait forever. I would like to let it wait max. 1 second and if the #buy btn is not showing up after that 1 second, it should still go to try.

So what I want to achieve is a flexible sleep timer between clicking #search and the search result that waits exaclty until the element can be clicked, but not longer than 1 second.

exec85
  • 447
  • 1
  • 5
  • 21

1 Answers1

-1

When you use await you need to make function async to constantly listening listen for events.

like mentioned in this tread

Shaheer Wasti
  • 65
  • 1
  • 10
  • I would like to completely delete the await sleep() and exchange it with a flexible wait time that waits for an element to be clickable. Thats the reason for my post here :-) – exec85 Oct 20 '21 at 07:36