I'm trying to record screen when playing a website by using mss and opencv, but I don't want the program to use the current screen. I want to put them to play on a second desktop, like Desktop 2 in the following picture macos have 4 desktop setup
so I can work in the desktop 1 without any interruption.
Currently, MSS does not support capturing inactive workspaces.
This Ask Ubuntu answer indicates (referencing a post on an "archived" (i.e. apparently deleted) forum) that this is not normally possible in the X Window System.1 The answer discusses using Xvfb as a workaround, but this does not appear to be useful for screen-capture software, as it is essentially a way to run the application on a virtual display, which can then be captured normally.
If taking a screenshot of an inactive space is possible on macOS (which I consider unlikely for the same reasons as on X), you would likely need to use non-API functions from CoreGraphics (as there was no public API for spaces as of 2016). This GitHub repository documents those functions, though the repository was last updated in 2016, so it may not be as helpful as you would like.
Another option, which may be the least OS-dependent, is to run a headless virtual machine and take screenshots of that. How well this works will depend on the virtual machine manager and the virtual machine itself, as well as how you are taking the screenshots.
1 Basically because the inactive desktops are not rendered.
For anyone who sees this question while looking for a way to take screenshots of other monitors, look at the edit history of this answer.