2

I need to run a javascript code inside Python. This JS code uses document DOM object functions and I don't know how to "create a DOM object" inside JS code to use this.

Currently I'm calling the JS file like:

import json
import PyV8

def runJS(html):
    ctxt = PyV8.JSContext()
    ctxt.enter()
    ctxt.eval(open('JScode.js').read().decode('utf8'))
    document = {'body': html.decode('utf8')}
    result = ctxt.eval('run(%s);' % json.dumps(document))

And in the JScode.js file I have a function like:

function run(html) {
    // TODO: convert html.body string to document
    // The html param is a dict object

    result = work(document);
    return result;
}

In the JScode.js I use methods like:

document.getElementById();
document.getElementByTagName();
document.evaluate();
document.querySelectorAll();

How can I make this work?

0 Answers0