Trouble with the Execute JavaScript in Safari


I am trying to use the Execute JavaScript in Safari-action to get some information from a webpage.
This is the code

It works if I execute it in the console in Safari.
But when running it in KM it does not return anything.

I have made a video of the process.

I can not give access to the page, but I have used this JavaScript (which works in KM)
to get a sample HTML.

There is not anything in the logs which shows an error.

Hope somebody can point me in the right direction to get this to work.

Hey Jimmy,

Can you save a webarchive from Safari instead of the bare html?


Have you enabled JavaScript access for AppleEvents?

Hi Peter

Yes. JavaScript access for AppleEvents is enabled.
I have also tried the same JavaScripts in Chrome and it is the same.
Nothing returns for


Hi Chris

Here is a webarchive of the page:

1 Like

I suspect it is because document.getElementsByClassName is returning a bunch of nodes, not a string that can be put in to a variable.

Try document.getElementsByClassName(“ui-dialog-content”).outerHTML (or .innerHTML or some such).

Hi Peter

Unfortunately this did not work.
At my company we have some web-developers and they have also tried to help me to find the best JavaScript to extract the data.

My own test now has in the JavaScript Console in Safari lead me to this code, which has a narrowed my data down.

But it does still not return anything from Keyboard Maestro.

That will not return anything to KM because it does not return text.
It returns an array of elements that have the specified class.

Try this:

var elemList = document.getElementsByClassName("ui-dialog-content");
// OR

As I said, getElementsByClassName returns an array of node elements. This is something Safari’s console can display in some form, but not something that makes any sense in a Keyboard Maestro variable.

So you need to convert it from that array of nodes in to whatever format you actually want. For example, something like this: document.getElementsByClassName("ui-jqgrid-bdiv"), function(obj){ return obj.innerHTML; }).join('')

This works in a Keyboard Maestro Execute JavaScript in Safari action, and returns something, but whether it is what you want for an array of nodes, I have no idea.

Hi JMichaelTX


This lead my down the right path.

var elemList = document.getElementById("JobTicketLineItems");

The above code return the correct table I need.

Thanks again.

I have said it before, but this forum is the best I have ever participated in.