Windows10虚拟机RDP环境下PyAutoGUI断开远程报错的解决办法求助
Fixing "OSError: screen grab failed" When Running PyAutoGUI on Windows 10 VM After RDP Disconnect
I’ve dealt with this exact frustration before—Windows immediately drops the session’s display output once you disconnect RDP, which breaks PyAutoGUI’s screen-grabbing functionality. Here are three reliable temporary solutions that should get your script running uninterrupted:
Solution 1: Use Group Policy to Force Persistent Session Display
This configures your VM to keep its active display even after you disconnect RDP:
- Open the Local Group Policy Editor on your VM by running
gpedit.mscfrom the Start menu or Run dialog. - Navigate to Computer Configuration > Administrative Templates > Windows Components > Remote Desktop Services > Remote Desktop Session Host > Session Time Limits.
- Locate Set time limit for disconnected sessions and set it to Disabled.
- Repeat this for Set time limit for active but idle Remote Desktop Services sessions and End disconnected sessions.
- Apply changes immediately by running
gpupdate /forcein Command Prompt, then restart your VM if required.
Solution 2: Connect via the Admin/Console Session
Windows has a dedicated console session that doesn’t disable display on disconnect:
- On your local machine, open Command Prompt and run
mstsc /admin(older Windows versions usemstsc /consoleinstead). - Use this RDP window to connect to your VM. When you close the RDP window, the VM’s session will stay active with its display intact, letting PyAutoGUI run without errors.
Solution 3: Add a Virtual Display Adapter
If you don’t have access to group policy settings, a virtual display tricks Windows into maintaining an active screen:
- First, install your VM’s guest tools (like VMware Tools or VirtualBox Guest Additions)—this enables virtual display support.
- For VirtualBox: Go to VM Settings > Display, check Enable VRDP, set the number of monitors to 2, then start the VM. Inside the VM, extend your desktop to the new virtual display.
- For VMware: Go to VM Settings > Display, increase the number of monitors, enable Accelerate 3D graphics if needed, then configure the virtual display in Windows’ display settings.
Once set up, disconnecting RDP won’t disable this virtual display, so PyAutoGUI can continue grabbing screen content from it.
Why Your Previous Attempts Didn’t Work
- The Terminals tool’s display-persist settings are often hidden or deprecated in newer versions, which is why you couldn’t locate them.
- The X11 display code you tried is designed for Linux environments—PyAutoGUI on Windows relies on Win32 APIs instead, so that snippet won’t function here.
内容的提问来源于stack exchange,提问作者Lore
相关产品推荐
相关产品推荐

