如何在Windows系统下使用Python更新Chrome浏览器?
Great question—dealing with Chrome/ChromeDriver compatibility mismatches is one of the most frustrating parts of browser automation. Let’s walk through how to handle both Chrome browser updates and driver syncing on Windows using Python.
Chrome doesn’t expose a native Python API for updates, but we can leverage Windows’ command-line tools (PowerShell) via Python to trigger Chrome’s built-in update mechanism. Chrome has a hidden --update flag that forces it to check for and install the latest version.
Here’s a script that calls PowerShell to initiate a Chrome update:
import subprocess def trigger_chrome_update(): try: # PowerShell command to run Chrome's update check update_command = """ $chrome_path = "${env:ProgramFiles(x86)}\Google\Chrome\Application\chrome.exe" if (Test-Path $chrome_path) { Start-Process -FilePath $chrome_path -ArgumentList "--update" -NoNewWindow -Wait Write-Host "Chrome update check completed." } else { Write-Host "Chrome not found at default installation path." } """ # Execute the PowerShell command from Python result = subprocess.run( ["powershell", "-Command", update_command], capture_output=True, text=True, check=True ) print(result.stdout) except subprocess.CalledProcessError as e: print(f"Update failed with error: {e.stderr}") except Exception as e: print(f"Unexpected issue: {str(e)}") # Run the update function trigger_chrome_update()
Important Notes:
- Admin Rights: Chrome updates often require administrator privileges. Make sure to run your Python script as an administrator (right-click the script > "Run as administrator").
- Default Path: This assumes Chrome is installed in the standard
Program Files (x86)location. If you have a custom install path, adjust the$chrome_pathvariable accordingly.
Even after updating Chrome, you’ll need a matching ChromeDriver version to avoid compatibility errors. The easiest way to handle this automatically is with the webdriver-manager library—it detects your installed Chrome version and downloads the exact matching driver.
First, install the required packages:
pip install selenium webdriver-manager
Then use this script to initialize Chrome with the correct driver:
from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager # Automatically install and use the matching ChromeDriver driver = webdriver.Chrome(ChromeDriverManager().install()) # Example: Navigate to a page driver.get("https://example.com") # Clean up when done # driver.quit()
To ensure you always have the latest Chrome and matching driver, combine the update and driver initialization:
import subprocess from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager def update_chrome(): try: update_cmd = """ $chrome_path = "${env:ProgramFiles(x86)}\Google\Chrome\Application\chrome.exe" if (Test-Path $chrome_path) { Start-Process -FilePath $chrome_path -ArgumentList "--update" -NoNewWindow -Wait Write-Host "Chrome is now up to date (if an update was available)." } else { Write-Host "Chrome installation not found." } """ subprocess.run( ["powershell", "-Command", update_cmd], capture_output=True, text=True, check=True ) except Exception as e: print(f"Chrome update warning: {str(e)}") # Step 1: Update Chrome to latest version update_chrome() # Step 2: Launch Chrome with compatible driver driver = webdriver.Chrome(ChromeDriverManager().install()) driver.get("https://your-target-page.com") # Don't forget to quit the driver when finished # driver.quit()
This workflow ensures you never run into version mismatches again—Chrome gets updated first, then we pull the exact driver version it needs.
内容的提问来源于stack exchange,提问作者Rahul

