44from selenium import webdriver
55from selenium .webdriver .chrome .options import Options
66from selenium .webdriver .chrome .service import Service
7+ from subprocess import PIPE , STDOUT
78print ("Gradio app loaded." )
89
910def capture_page (url : str , output_file : str = "screenshot.png" ):
@@ -14,15 +15,31 @@ def capture_page(url: str, output_file: str = "screenshot.png"):
1415 :param output_file: The filename to save the screenshot.
1516 """
1617 options = Options ()
17- options .add_argument ("--headless" ) # Run in headless mode
18- options .add_argument ("--window-size=1920,1080" ) # Set window size
18+ options .add_argument ('--headless' )
19+ options .add_argument ('--no-sandbox' ) # Required in Docker
20+ options .add_argument ('--disable-dev-shm-usage' ) # Required in Docker
21+ options .add_argument ('--disable-gpu' ) # Required in Docker
22+ options .add_argument ('--disable-software-rasterizer' )
23+ options .add_argument ('--window-size=1920,1080' )
24+ options .add_argument ('--disable-extensions' )
25+ options .add_argument ('--disable-infobars' )
1926
20- # Initialize Chrome service
21- service = Service ()
22- driver = webdriver .Chrome (service = service , options = options )
27+ # Set up Chrome service with explicit path to chromedriver and logging
28+ service = Service (
29+ executable_path = '/usr/local/bin/chromedriver' ,
30+ log_output = PIPE # Redirect logs to pipe
31+ )
32+
33+ # Initialize Chrome with the service and options
34+ driver = webdriver .Chrome (
35+ service = service ,
36+ options = options
37+ )
2338
2439 try :
2540 driver .get (url )
41+ # Add a small delay to ensure page loads completely
42+ driver .implicitly_wait (5 )
2643 driver .save_screenshot (output_file )
2744 print (f"Screenshot saved: { output_file } " )
2845 finally :
@@ -44,6 +61,7 @@ def capture_and_show(url: str):
4461 # Return the image path
4562 return temp_path
4663 except Exception as e :
64+ print (f"Error in capture_and_show: { str (e )} " ) # Add detailed logging
4765 return f"Error capturing page: { str (e )} "
4866
4967def create_gradio_app ():
0 commit comments