Chrome浏览器在RemoteDesktop连接VM最小化后Selenium焦点丢失问题求助
Hey Kumar, I’ve run into this exact headache before when working with Selenium on VMs via Remote Desktop—let’s break down why it’s happening and how to fix it quickly.
Why This Happens
When you minimize the Remote Desktop window, your VM’s OS usually throttles or pauses UI interactions for non-active windows. Chrome detects it’s no longer in focus and stops processing input events from Selenium, which is why your script can’t enter usernames or passwords anymore.
Solutions to Try
1. Use Chrome’s Headless Mode (Most Reliable)
Headless mode runs Chrome without a visible UI, so it doesn’t care if the VM window is minimized or hidden. This is the simplest fix for background execution.
Here’s how to set it up in your Java code:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; public class LoginScript { public static void main(String[] args) { ChromeOptions options = new ChromeOptions(); // Enable headless mode options.setHeadless(true); // Required for Windows systems to avoid GPU-related errors options.addArguments("--disable-gpu"); // Set a window size to ensure elements render correctly options.addArguments("--window-size=1920,1080"); // Optional: Disable sandbox for better VM compatibility options.addArguments("--no-sandbox"); WebDriver driver = new ChromeDriver(options); driver.get("your-login-page-url"); // Your login logic here (username/password input) // ... driver.quit(); } }
2. Force Chrome to Ignore Focus State
If you need a visible browser window (not headless), add Chrome flags that prevent it from pausing when the window is out of focus.
Update your ChromeOptions with these arguments:
ChromeOptions options = new ChromeOptions(); // Stop Chrome from backgrounding when the window is occluded (minimized) options.addArguments("--disable-backgrounding-occluded-windows"); // Keep the renderer active even when the window isn't focused options.addArguments("--disable-renderer-backgrounding"); // Start Chrome maximized to avoid initial focus issues options.addArguments("--start-maximized"); WebDriver driver = new ChromeDriver(options);
These flags tell Chrome to keep processing UI events regardless of window focus, so your script can interact with elements normally even if the VM is minimized.
3. Adjust Remote Desktop Session Settings
If the above fixes don’t work, tweak your Remote Desktop and VM settings to prevent session suspension:
- When connecting to the VM, click Show Options > Local Resources > Under "Remote audio", select "Do not play" and "Do not record" (reduces session resource usage).
- On the VM, open Local Group Policy Editor (run
gpedit.msc):- Navigate to
Computer Configuration > Administrative Templates > Windows Components > Remote Desktop Services > Remote Desktop Session Host > Session Time Limits - Set Set time limit for disconnected sessions to "Never"
- Disable Set time limit for active but idle Remote Desktop Services sessions
- Navigate to
This ensures the VM’s session stays active even when you minimize the Remote Desktop window.
内容的提问来源于stack exchange,提问作者Kumar Thoudishetti

