@@ -19,8 +19,9 @@ def capture_page(url: str, output_file: str = "screenshot.png"):
1919 :param output_file: The filename to save the screenshot.
2020 """
2121 options = Options ()
22+
2223 # Basic options
23- options .add_argument ('--headless' )
24+ options .add_argument ('--headless=new' ) # New headless mode
2425 options .add_argument ('--no-sandbox' ) # Required in Docker
2526 options .add_argument ('--disable-dev-shm-usage' ) # Required in Docker
2627
@@ -32,16 +33,24 @@ def capture_page(url: str, output_file: str = "screenshot.png"):
3233
3334 # Resource configuration
3435 options .add_argument ('--window-size=1920,1080' )
35- options .add_argument ('--remote-debugging-port=9222' ) # Fix DevTools port issue
36- options .add_argument ('--disable-features=site-per-process' ) # Reduce memory usage
37- options .add_argument ('--memory-pressure-off' ) # Prevent memory-related crashes
36+ options .add_argument ('--disable-features=NetworkService,NetworkServiceInProcess' )
37+ options .add_argument ('--disable-features=site-per-process' )
38+
39+ # Memory and process settings
40+ options .add_argument ('--single-process' ) # Run in single process mode
41+ options .add_argument ('--memory-pressure-off' )
42+ options .add_argument ('--disable-crash-reporter' )
43+ options .add_argument ('--disable-breakpad' ) # Disable crash reporting
3844
3945 # Additional stability options
4046 options .add_argument ('--ignore-certificate-errors' )
41- options .add_argument ('--allow-insecure-localhost' )
4247 options .add_argument ('--disable-setuid-sandbox' )
4348 options .add_argument ('--disable-web-security' )
4449
50+ # Set specific shared memory /dev/shm size (if needed)
51+ options .add_argument ('--disable-dev-shm-usage' )
52+ options .add_argument ('--shm-size=2g' )
53+
4554 # Set up Chrome service with explicit path to chromedriver and logging
4655 service = Service (
4756 executable_path = '/usr/local/bin/chromedriver' ,
@@ -83,7 +92,18 @@ def capture_page(url: str, output_file: str = "screenshot.png"):
8392 raise
8493 finally :
8594 print ("Closing Chrome..." )
86- driver .quit ()
95+ try :
96+ driver .close () # Close current window
97+ driver .quit () # Quit browser completely
98+ import psutil # For process cleanup
99+ current_pid = os .getpid ()
100+ current_process = psutil .Process (current_pid )
101+ children = current_process .children (recursive = True )
102+ for child in children :
103+ if 'chrome' in child .name ().lower ():
104+ child .terminate ()
105+ except Exception as cleanup_error :
106+ print (f"Error during cleanup: { cleanup_error } " )
87107
88108 except Exception as e :
89109 print (f"Error initializing Chrome: { str (e )} " )
0 commit comments