Google Colab提前超时断开,常用修复及代码方案失效求解决
Hey there, sorry to hear you're stuck with Colab disconnecting way earlier than expected—those random timeouts are super frustrating. Let's walk through some fixes, starting with the broken script issue and moving to other reliable methods.
Fix the Broken Auto-Connect Script
The original script you used relies on an outdated DOM selector (#connect)—Colab updates its UI regularly, so that element no longer exists. Here's an updated version that works with the current Colab interface (as of 2024):
function keepColabAlive() { console.log("Keeping Colab session alive..."); // Target the current connect button element const connectBtn = document.querySelector('colab-connect-button') || document.querySelector('#top-toolbar colab-connect-button'); if (connectBtn) { connectBtn.click(); } // Simulate mouse movement to stop the browser from marking the tab as idle const mouseMove = new MouseEvent('mousemove', { bubbles: true, cancelable: true, clientX: Math.random() * window.innerWidth, clientY: Math.random() * window.innerHeight }); document.dispatchEvent(mouseMove); } // Run every 5 minutes to avoid triggering anti-bot safeguards setInterval(keepColabAlive, 300000);
Why Your Original Script Threw an Error
You probably saw a console error like this:
Uncaught TypeError: Cannot read properties of null (reading 'click')
This happens because document.querySelector("#connect") returns null—that element doesn't exist anymore. The updated script targets the actual colab-connect-button component that Colab uses now, and adds mouse movement to prevent your browser from throttling the inactive tab (a common cause of early timeouts).
No-Script Workarounds
If you'd rather avoid messing with browser dev tools, try these low-effort tricks:
- Keep the Colab tab active at all times. Browsers throttle background tabs, which tells Colab your session is idle and triggers a disconnect.
- Use a simple browser automation tool to simulate small interactions: for example, set a tool to click inside the notebook every 20 minutes, or scroll up/down slightly.
- Physically interact with the tab every 30 minutes: click a code cell, scroll through the notebook, or even type a single character (you don't need to run the cell).
Optimize Runtime Settings for Longer Sessions
- Go to Runtime > Change runtime type and ensure you've selected the appropriate hardware (GPU/TPU if you're doing heavy compute). Free tier sessions have stricter limits, but even free sessions last longer if you're using resources actively.
- Close unused resources: shut down plots, widgets, or idle processes in the runtime. Colab is more likely to terminate sessions that are hoarding resources without doing actual work.
Paid Tier Perks (If You Can Upgrade)
Colab Pro/Pro+ gives you longer session limits (up to 24 hours) and priority access to resources. Paid tiers also have more robust anti-timeout systems, so you're far less likely to get disconnected prematurely.
内容的提问来源于stack exchange,提问作者T. Rex

