You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何运行多个带独立Cookie的隐藏Chrome窗口并按需显示

实现多独立Cookie Chrome窗口的Python脚本

Got it, let's build this script together. The core idea is leveraging Chrome's separate user data directories to keep cookies isolated between windows—just like manually creating person1/person2 profiles. We'll add input for window count, target URL, and a condition check so windows only pop up when your trigger is met.

Full Script

import os
import subprocess
import sys
import requests  # Use this for condition checks (install via `pip install requests` if needed)
import time  # For rate limiting (optional but recommended)

def check_condition(target_url):
    """Replace this with your actual condition logic.
    Example: Check if a product is available on the target site."""
    try:
        response = requests.get(target_url, timeout=10)
        # Example condition: Look for an "Add to Cart" button in the page source
        return "Add to Cart" in response.text
    except Exception as e:
        print(f"Error checking condition: {e}")
        return False

def main():
    # Get user input
    try:
        window_count = int(input("Enter number of Chrome windows to open: "))
        target_url = input("Enter the target URL to open in all windows: ").strip()
        if not target_url.startswith(("http://", "https://")):
            print("Please enter a valid URL starting with http:// or https://")
            sys.exit(1)
    except ValueError:
        print("Please enter a valid number for window count")
        sys.exit(1)

    # Wait until condition is met
    print("Waiting for condition to be satisfied...")
    while not check_condition(target_url):
        time.sleep(5)  # Avoid spamming the target server

    # Define Chrome executable path (adjust based on your OS)
    if sys.platform == "win32":
        chrome_path = "C:/Program Files/Google/Chrome/Application/chrome.exe"
    elif sys.platform == "darwin":
        chrome_path = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
    else:  # Linux
        chrome_path = "google-chrome"

    # Create profiles directory if it doesn't exist
    profiles_dir = os.path.join(os.getcwd(), "chrome_profiles")
    os.makedirs(profiles_dir, exist_ok=True)

    # Launch each Chrome window with unique profile
    for i in range(window_count):
        profile_dir = os.path.join(profiles_dir, f"person{i+1}")
        # Command to launch Chrome with isolated profile
        cmd = [
            chrome_path,
            "--user-data-dir=" + profile_dir,
            "--new-window",
            target_url
        ]
        # Start the process (detach so script doesn't wait for Chrome to close)
        subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        print(f"Launched Chrome window {i+1} with profile: {profile_dir}")

if __name__ == "__main__":
    main()

Key Details Explained

  • Isolated Cookies: Each window uses a unique --user-data-dir (stored in chrome_profiles/personX), so cookies, bookmarks, and settings are completely separate—exactly like manual user profiles.
  • Condition Check: The check_condition function is a placeholder. Replace it with your actual logic:
    • Parse HTML/API responses to check if a product is in stock
    • Wait for a specific timestamp
    • Monitor a webhook for a trigger event
  • Input Handling: The script asks for window count and target URL, with basic validation to avoid invalid inputs.
  • Cross-OS Support: Automatically uses the correct Chrome executable path for Windows, Mac, or Linux.

Important Notes

  • Chrome Path: If your Chrome is installed in a non-default location, update the chrome_path variable to match.
  • Rate Limiting: The time.sleep(5) in the condition loop prevents hitting the target website too frequently (avoids getting blocked).
  • Dependencies: If using requests for condition checks, install it first with pip install requests.
  • First Launch: When you run the script for the first time, Chrome will ask you to set up each profile (name/avatar)—this is normal, and subsequent launches will reuse the saved profile.

内容的提问来源于stack exchange,提问作者user9510595

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:18:32