Chrome Driver Environment Setup
For setting up Chrome driver environment, ensure that the Chrome browser version matches the driver version. This guide assumes Chrome version 120 and its compatible driver. A straightforward method is used, which is verified to work for versions up to 120.
Launching Chrome with Persistent Cache
Using a cached session allows Selenium to mimic a manual opened browser instance, preserving user data and login states.
Configuration for Cache-Preserving Launch
To launch Chrome with existing cache data, first locate the Chrome user data directory using chrome://version/. Use only the path up to the User Data folder.
chrome_data_path = r'C:\Users\Cassie\AppData\Local\Google\Chrome\User Data'
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=' + chrome_data_path)
driver = webdriver.Chrome(executable_path=chromedriver_path, options=options)
Automatically Closing Existing Browser Instances
Before launching a new instance, ensure no existing Chrome or Chromedriver processes are running:
os.system('taskkill /im chromedriver.exe /F')
os.system('taskkill /im chrome.exe /F')
Suppressing Chrome Crash Recovery Prompt
Upon restart, Chrome may show a crash recovery prompt. This is resolved by modifying the Preferences file to indicate a normal exit:
def set_exit_normal():
preferences_path = os.getenv("LOCALAPPDATA") + r'\Google\Chrome\User Data\Default\Preferences'
with open(preferences_path, 'r', encoding='utf8') as pf:
preferences = json.loads(pf.read())
preferences['profile']['exit_type'] = 'Normal'
with open(preferences_path, 'w', encoding='utf8') as pf:
pf.write(json.dumps(preferences, ensure_ascii=False, separators=(',', ':')))
Final Implementation
Driver Utility Module
The following module provides two methods for launching Chrome: standard or with persistent cache:
import json
import os
from selenium import webdriver
chromedriver_path = r"C:\Program Files\Google\Chrome\Application\chromedriver.exe"
chrome_data_path = r'C:\Users\Cassie\AppData\Local\Google\Chrome\User Data'
def set_exit_normal():
preferences_path = os.getenv("LOCALAPPDATA") + r'\Google\Chrome\User Data\Default\Preferences'
with open(preferences_path, 'r', encoding='utf8') as pf:
preferences = json.loads(pf.read())
preferences['profile']['exit_type'] = 'Normal'
with open(preferences_path, 'w', encoding='utf8') as pf:
pf.write(json.dumps(preferences, ensure_ascii=False, separators=(',', ':')))
def kill_chrome():
set_exit_normal()
os.system('taskkill /im chromedriver.exe /F')
os.system('taskkill /im chrome.exe /F')
def drivers(cache=False):
if not cache:
return webdriver.Chrome(chromedriver_path)
else:
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=' + chrome_data_path)
return webdriver.Chrome(executable_path=chromedriver_path, options=options)
Usage Example
from time import sleep
from driver_ import drivers, kill_chrome
try:
driver = drivers(True)
except:
kill_chrome()
driver = drivers(True)
sleep(2)
driver.get('https://blog.csdn.net/aaaaaaaaanjjj')
Dependencies and Packaging
- Python 3.7
- Selenium
- Chrome version 120
Disabling Chrome Auto-Udpate
Two methods are available to prevent automatic updates:
- Modify group policy settings to disable automatic updates.
- Rename the
GoogleUpdate.exefile located in the Google Update directory (e.g.,C:\Users\lenovo\AppData\Local\Google\Update). Note that this may prevent critical security updates from being applied.