Copy text from a webpage using Javascript

I know this will be a repeat of previous topics, but here it goes: I am trying to copy the blue-highlighted field in a webpage to a text variable in KM. In particular, I am interested in extracting "Statement | 2024-06-30" and putting it in a variable.

Screen Shot 2024-07-04 at 3.39.50 PM

The JS I am using below returns something, but not what I am looking for:

return document.querySelector("title").innerText

I have tried a lot of little variations with no luck. My HTML appears to be a little different from what I have seen in other threads on this topic. What is the best way forward?

I appreciate everyone's help. Let me know if I need to provide further info.

return document.querySelector("title").innerText

First thing that jumps to the eye there is that I would expect the id selector string to be prefixed by a hash character.

return document.querySelector("#title").innerText

Beyond that, can you expand:

something, but not what I am looking for


  1. What did you expect ?
  2. What did you see ?

When I exclude the #, I get the following result:

Screen Shot 2024-07-05 at 3.12.29 PM

When I include the #, it seems like a step back:

Screen Shot 2024-07-05 at 3.18.09 PM

I am trying to get the date of a statement as indicated below:

I am only interested in the date, but based on the HTML I am guessing I will just have to save the whole thing and separate out the date via other means (which sounds easy...). Another way to do this whole thing would be to extract the date from the PDF itself, but I am under the impression that such a method would be harder.

I do not see a "title" id anywhere else, so unfortunately I am not sure why I get "Unitus Community Credit Union." The line of code is seeing something I do not see.

Actually, I made some progress...I was able to get the following to work in the console. I just copied the JS path of the element I am looking for and appended it with ".innerText":


Now the problem is getting it to work in KM. Based on the thread below, I thought adding "return" at the beginning would help, but the variable I am saving to does not get filled. I am using KM 11.0.3 if that helps. Any thoughts on this?

Difficult to know what that means without seeing the macro – you can show us a copy by using File > Share > Keyboard Maestro Forum in the Keyboard Maestro menu system.

The exact macro is for a personal banking website, so I will refrain from sharing that one. However I was able to replicate the problem in a similar scenario, and I have attached the KM macro for that. This one is supposed to work by copying the text circled in red below to the clipboard. Interestingly, the JS line is exactly the same for this as for the eDocument I described above. Probably because it is the same online PDF view or whatever. The result is the same, though; when I run the macro, null is copied to the system clipboard.

Hopefully the attachment makes it, but please let me know if it is inaccessible.

copyDocumentTitle.kmmacros (2.9 KB)

That url is for the PDF

.querySelector methods are only defined in the Document Object Model for an HTML page that has been parsed by the browser.

(They have no meaning for a PDF file)

Looking at the html which you show – it appears to be in the JS evaluation space of a Chrome plugin – rather than that of the browser itself, and therefore (by design, I think) beyond the reach of external scripting through the Apple Events interface.

(Beyond the reach, in other words, of Keyboard Maestro Execute JS in Browser actions)

Technically, in fact, an extension, rather than a plugin. Just in case that is accessible to scripting, check that you have enabled, in the Chrome main menu:

View > Developer > Allow JavaScript from Apple Events

Thanks, I did allow JS from Apple Events before starting this work, and I could get other things to work, just not this particular thing. I wondered if if querySelector was impossible since it technically is not a webpage, and you provided the perfect answer. Thank you for your patience; I am just getting started with KM and while I have coding experience, it is not my primary background.

It will take a little more work to parse out, but I was easily able to get the date from the webpage where the PDF came from. If pulling from the PDF page had worked, it would have saved some steps due to being in the exact format I need, but thankfully I am not dead in the water or anything.

1 Like

So near and yet so far :slight_smile:

You can see the .querySelector match (for those plugin/extension contexts) in the console, which is inside the Chrome process, but you can't get scripting access to them from an external process. A pity in a way, but probably fortunate, in terms of security.