Trouble with the Execute JavaScript in Safari

Hi

I am trying to use the Execute JavaScript in Safari-action to get some information from a webpage.
This is the code
document.getElementsByClassName("ui-dialog-content");

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.
Video: http://d.pr/v/12M0v

I can not give access to the page, but I have used this JavaScript (which works in KM)
document.body.innerHTML
to get a sample HTML.
Sample-html: http://d.pr/f/1jJ57

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?

-Chris

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
document.getElementsByClassName("ui-dialog-content");

But
document.body.innerHTML
works

Hi Chris

Here is a webarchive of the page:
http://d.pr/f/9pQv

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.
document.getElementsByClassName("ui-jqgrid-bdiv").item(1);

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");
elemList[0].innerText;
// OR
elemList[0].innerHTML;

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:

Array.prototype.map.call( 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

Thanks.

This lead my down the right path.

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

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.

2 Likes