Chrome Headless Shell Script With Keyboard Maestro Actions Afterward On The URL?

I have a question about whether it is possible to combine headless Google Chrome with Keyboard Maestro's actions such as Execute Javascript in Google Chrome, Move and Click Mouse, among others.

Here's the scenario.

A macro I have created works but I run overnight - which you'll find out why. Long story short it retrieves a list of URLs from a web page, stores them inside a K.M Variable, and then I use the For Each action for The Lines in: 'MyVariable'
I have a Open URL action inside the For Each block with default application (my case, Chrome), where I am using %link% as the identifier for iterating over each URL. I then get K.M to perform various actions for each URL like Execute JavaScript in Google Chrome, keystrokes. etc.

My problem is, if there's hundreds of URLs (i'd like to scale this ..), I have to run it overnight when I'm not using the machine since the engine is running doing its thing but works.
I've tried to learn more about Google Chrome headless and found a thread relating to it here but couldn't work out whether what I want to do is possible from my own research/testing...

Is there a way to use For Each like I am doing currently, but instead of open the URL in Google Chrome app, do it headless BUT do all the K.M actions to the page headless, as if it was open in the app? Like the mouse clicks, keystrokes.. etc. I'm thinking it may not be possible UNLESS I could do something similar with Selenium?

Example of headless setup that opens one URL used in Execute a shell script:

alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome" 
chrome  --headless "https://www.google.com"

Couldn't seem to make further actions work trying like execute a external js file.

Hope this all makes sense but let me know if further info needed from me!

Are you trying to get this to work without a display, or just without a visible browser window?

Why not just test it (probably with Selenium) and see? I suspect it won't work, but I'm hoping it does. I strongly suspect that some KM actions like Find Image will not work. If Selenium supports the standard macOS key buffer, then the KM "key actions" might work. How can you expect mouse actions to work if the Chrome window is virtual and not on the display? (What coordinate system would make sense?) The more I think about it, the less likely I think it could work. But I'm not qualified to answer this question, and I have no plan to use Chrome or Selenium.

Without a visible browser window. I have a external display that's always connected by HDMI. Thinking back to projects in Python where I've used Selenium where when I enabled a headless Chrome window, it performed actions like clicking buttons, running snippets etc without me seeing what was happening in browser and I could do something else. I was just wondering how I could get KM to fit in with the equation before attempting. Learned recently how to use os.environ['KMVAR_VariableName'] recently with Python maybe that could work. As for coordinates for certain mouse clicks yeah that's what I was wondering. Worst case I can't get KM to do it maybe there's certain libraries out there I could try reproduce certain built-in actions.

It sounds like python may have the libraries needed to work with a headless Chrome. So you may want to stick with that. I don't recall anyone saying that they got KM to work with a headless Chrome.

If there's an upside to this, if you can get Python to control Chrome, then you can almost certainly use KM's Execute Shell Script action to execute python commands. Something like this: (assuming you have python installed on your Mac)

image

That would probably work.