Python → jxa → js in safari → output to python

I know I can use keyboard maestro to run JavaScript in Safari tab. however, I tried to do it from a python script. And I am failing. I think the script is failing because osascript arguments are not escaped.

Hope this is ok to ask here as it is not Keyboard Maestro question but is a Python / JXA / JS question.

My python code which runs when the desired page is open in Safari. In this case the desired page for example is https://www.w3schools.com/html/html_tables.asp

import subprocess

def run_jxa(jxa_script, data:list):
    args = ["osascript", "-l", "JavaScript", jxa_script]
    for x in data:
        args.append(x)
    output = subprocess.run(args, capture_output=True)
    return output

def main():
    script_file = "get_safari_element_jxa.js"
    with open(script_file, 'r') as f:
        jxa_script = f.read()

    js_for_safari_file = "get_safari_element.js"
    with open(js_for_safari_file, 'r') as f:
        js_for_safari = f.read()

    output = run_jxa(jxa_script, [js_for_safari])
    print(output)
    
if __name__ == "__main__":
    main()

File get_safari_element_jxa.js contains:

function run(argv) {
    'use strict';
    var safari = Application("Safari");
    var safari_script = argv[0];
    var output = safari.doJavaScript(safari_script, {
        in: safari.windows[0].currentTab
    })
    return output;
}

File get_safari_element.js contains:

function get_data(xpath){
    var elements = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    var element = elements.snapshotItem(0)
    return element.innerHTML;
}

xpath = '//*[@id="customers"]';
get_data(xpath);

If somebody can review the above code and advise how I can get the desired result, I would be grateful.

My primary script is in python because I know that the best. I do want to use doJavaScript function as that allows me to have full control over the open Safari page.

Thank you for reviewing the question and for your help.

Since KM can run JXA scripts and Python scripts and JavaScript in Browser scripts, I would suggest that you take advantage of KM to do to.
Combine the various scripts in a KM Macro.

@JMichaelTX Thank you for the quick reply. I will use KM.

And, if anybody here knows how python → JXA → Safari javascript calls can be made and can share some insight, that would be cool thing to learn.

how python → JXA → Safari javascript calls can be made

Not sure that that is yet a question that is sufficiently well-defined to be clearly answerable.

( You would need to define a minimum example of what you are broadly and specifically trying to do )

i.e. sample input and expected output.

Ok - let me try to define it.

Here is what I am trying to achieve.

  • process web page data in a python script (language I know the most)
  • the web page does not open easily in Selenium, therefore need Safari to open page.
  • would like to use javascript in Safari, because the web elements not always rendered until page scrolled with injected javascript.
  • Once page open, would like to run python script, that in turn will need either AppleScript or JXA to inject JavaScript into Safari. Once javascript extracts the element of interest, element.innerHTML to be stored in a variable in Python. I suppose string output of doJavascript in JXA can be stored in a JXA variable, and JXA can return the string to Python.

I hope this makes sense. Thank you for your time again.