Macro: Pause Until Web Page Element Exists [Example]

To provide you with more help, I need the answers to these:

Oh, Sorry about that.

The notification doesn't show up, and after some time the macro times out.

It can open the page using Google Chrome but doesn't detect the element in Chrome – it works perfectly with Safari.

Is h1 a reliable CSS_Selector for the URL https://finance.yahoo.com/quote/BASFY using Safari?

I have had inconsistent results using h1 as the Selector. The action "PAUSE Until HTML Element is Present in FrontMost Web Page" sometimes times out. The problem could be elsewhere, but my confidence in the Selector is my initial suspect.

Yes, although with such a common tag as h1 I would probably add to it with one of these:
h1.D\\(ib\\)

OR
div#mrt-node-Lead-3-QuoteHeader h1.D\\(ib\\)

I doubt that the CSS_Selector has anything to do with the timeout. Most likely due to something else, like slow Internet, slow web server, etc.
I would just increase the timeout to the max time you have observed it takes to load the page.

I'll try both, thanks.

This Macro is great. Works ok most of time!

Recently I found that this macro doesn't work on my one of the pages I worked on. I found that page has an iframe that may cause this macro stops working, even though the element I am waiting for is NOT inside of that iframe. I posted here:

Difficulty focus/click an input textbox in AWS cloudwatch log webpage

Anyone has experienced on such issue? Any alternative way to wait an element exists on webpage that has iframe?

Thanks!

iframes are NOT accessible via JavaScript in Browser.
AFAIK, iframe should not cause a problem if you are NOT trying to access it.
Have you tested your JavaScript in the Chrome JavaScript console?

Sometimes complicated web pages require a bit of trial-and-error when using JavaScript.
The best way way is to test in the Chrome JavaScript console.

Tried in chrome console too, not able to find the element. even though the element is not inside the iframe.

Really not sure why :frowning:

That means the CSS for the querySelector is not correct.
Keep testing.

Sorry I took it back, after dump the whole html code and check, the element does inside another iframe.

1 Like

This macro works for the frontmost web page, is it possible to wait element on the specific tab of the browser instead of the front most page? This would be super helpful for some slow loading webpages so that we can do something else by switch to other apps and don't have to wait on the browser.

Since this macro uses the KM JavaScript in Front Browser, and KM does not support JavaScript in browser pages/windows that are NOT frontmost, it would require a script (AppleScript or JXA) to address non-front tabs.

This is doable, but complex, and beyond the scope of this macro.
So it would be a task that the user would need to do.

2 Likes

Is this macro working for anyone else?

For me it just gets caught looping through the pause for 0.1 sec -> check for selector step, even after the page and selector have clearly loaded.

I've tested with several different elements on several different pages, and in both Chrome and Safari.

Example page and the selector you're looking for?

To test I tried:

https://forum.keyboardmaestro.com/

li.header-dropdown-toggle.search-dropdown

Works for me in Safari 14.1.2 (Mojave). Post what OS you are on and I'll see if I've got a tester handy. And post your failing macro, just in case...

Thanks for your help.

I have Safari 16.3 on Ventura 13.2.1

And here's the macro:

Pause test macro.kmmacros (11.0 KB)

...which works fine for me on Safari 16.1/macOS 13.0.1

I'll try and put through the updates on this machine later. Meanwhile, I could reproduce the problem by turning off Safari's "Allow JavaScript from Apple Events" in the Develop menu -- so worth a quick check to make sure you haven't inadvertently done the same.

1 Like

Aha, this works!

Issue is solved for both Safari and Chrome.

Thanks so much for your help.

Thanks for creating this macro. I converted it into a subroutine. It is invaluable!

Chris