Taking screenshots with the command line of a specific window
To capture a window using screencapture in Terminal, you have two options:

Interactively

-i         capture screen interactively, by selection or window
               control key - causes screen shot to go to clipboard
               space key   - toggle between mouse selection and
                             window selection modes
               escape key  - cancels interactive screen shot


You can use the -o option in conjunction with the -i option to not capture the shadow of the window, e.g.: -oi

Programmatically

-l&lt;windowid&gt; capture this windowsid


The -i option appears to be self-explanatory while the -l&lt;windowid&gt; option gives no additional info as with the -i option.



The &lt;windowid&gt;, for some windows can be ascertained by using AppleScript in the following example:

screencapture -l$(osascript -e 'tell app "Safari" to id of window 1') test.png


You can use the -o option in conjunction with the -l option to not capture the shadow of the window, e.g.: -ol

Some applications will not work with this method, however here are some alternatives:


screenshot: Better macOS screenshots via the Terminal
GetWindowID


Reference: How do I find the windowid to pass to screencapture -l?

Note: I am not affiliated with these projects.