python uses execjs and jsdom to execute js code solutions containing documents and set environment variables

Let’s talk about the environment first: win7 64-bit, python3.8.10.

When python uses execjs to execute js code containing document, an error will appear:

execjs._exceptions.ProgramError: TypeError: 'document' is undefined

The reason is that pure js code does not contain objects in the browser (such as document, window, etc.). In this case, a library called jsdom is needed. The global installation command is:

npm i jsdom -g

First you need to install npm, which is the package manager under nodejs.

1. Login URL Node.js

Download and install

As a result, the following problems occurred, which require at least Windows 8.1.

Try to download an earlier version, historical version download URL Previous Releases | Node.js

Download and unzip, my path is (C:\Users\Administrator\Downloads\
ode-v13.14.0-win-x64\
ode-v13.14.0-win-x64), where node_modules is the module and node.exe is the nodejs program , npm.cmd is the package manager.

cmd into this directory, run node -v and npm -v, OK.

Now, cmd into the npm path to install jsdom.

npm i jsdom -g

The result is an error, which seems to be a version problem:

After various inquiries and research, the 19.0.0 version of jsdom can be used. You can view various versions of jsdom through the following URL.

https://www.npmjs.com/package/jsdom?activeTab=versions

https://www.npmjs.com/package/jsdom/v/19.0.0?activeTab=readme

The command to install the specified version (-g refers to global installation) is as follows:

npm i [email protected] -g

After the installation is complete, cmd enters the node path and runs node.

Enter the commands in sequence, and you can see that there is an environment that recognizes window and document.


  1. const jsdom=require("jsdom");

  2. const {JSDOM}=jsdom;

  3. const dom = new JSDOM('

    Hello world

    ');

  4. dom.window

  5. dom.window.document

The way to exit node is the shortcut key: ctrl + c, which seems to need to be pressed twice.

At this point, jsdom has been successfully installed on this machine. Next, you need to use python and execjs to use it. Talk about it in the next section.

Note: To add node and npm to environment variables, right-click on My Computer-Properties-Advanced System Settings-then operate as shown below

After editing, there should be content in it. Add an English “;” at the end of the existing content, and then change the paths of node and npm to C:\Users\Administrator\Downloads\
ode-v13.14.0-win-x64\
ode-v13 Just fill in .14.0-win-x64

========================

After installing jsdom, you can program and enter the code directly:


  1. import execjs#pip3.exe install PyExecJS

  2. print(execjs.get().name)

  3. js='''

  4. const jsdom = require("jsdom");

  5. const { JSDOM } = jsdom;

  6. const dom = new JSDOM(`

    Hello world

    `);

  7. window = dom.window;

  8. document = window.document;

  9. XMLHttpRequest = window.XMLHttpRequest;

  10. function signature(a,b){

  11. return a + b;

  12. }

  13. '''

  14. ctx = execjs.compile(js,cwd='C:/Users/Administrator/Downloads/node-v13.14.0-win-x64/node-v13.14.0-win-x64/node_modules/npm/node_modules')

  15. result = ctx.call('signature',2,3)

  16. print(result)

Because jsdom is installed globally, cwd=’package path’ is required in the code. Note: This refers to the path of node_modules in npm, not the path of node_modules in jsdom. As shown in the picture:

Let’s do something more complicated, using jsdom to locate a specified element in a piece of xml code and get the corresponding content.


  1. import execjs#pip3.exe install PyExecJS

  2. js='''

  3. const jsdom = require("jsdom");

  4. const { JSDOM } = jsdom;

  5. const dom = new JSDOM(`

  6. This is a text, about something, that happened on

  7. 17.November 2023

  8. . It is a very important date.

  9. `,{contentType: "application/xml"});

  10. window = dom.window;

  11. document = window.document;

  12. XMLHttpRequest = window.XMLHttpRequest;

  13. const dateEl = document.evaluate("PARAGRAPH/DATE", document, null, 9, null).singleNodeValue;

  14. const date = dateEl.getAttribute("ISO");

  15. function signature(){

  16. return date;

  17. }

  18. '''

  19. ctx = execjs.compile(js,cwd='C:/Users/Administrator/Downloads/node-v13.14.0-win-x64/node-v13.14.0-win-x64/node_modules/npm/node_modules')

  20. result = ctx.call('signature')

  21. print(result)

Result output: 20131117.

Others have not been studied yet. jsdom should generally be used in reverse js programming.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeHomepageOverview 384019 people are learning the system